Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8154977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:31:49+00:00 2026-06-06T16:31:49+00:00

I have a Discussion (polymorphic resource) that can belong to Project, Task and Subtask.

  • 0

I have a Discussion (polymorphic resource) that can belong to Project, Task and Subtask. I have troubles testing the following create action:

 26   def create               
 27     @discussion = @parent.discussions.build(params[:discussion])
 28     @discussion.user_id = current_user.id
 29     if @discussion.save    
 30       current_user.discussions.push(@discussion)
 31       redirect_to [@parent, @discussion], :notice => 'Discussion started' 
 32     else                   
 33       render 'new'         
 34       flash.now[:alert] = 'Unable to start discussion'
 35     end                    
 36   end

These are before filters that happen before create action (these find the necessary things)

 63   private
 64 
 65   def find_cached_parent_or_from_something_id
 66     @parent || find_parent_from_something_id # this does a bit of caching
 67   end
 68 
 69   def find_parent_from_something_id
 70     @parent = nil
 71     params.each do |name, value|
 72       if name =~ /(.+)_id$/
 73         @parent = name.humanize.constantize.find(value)
 74       end
 75     end
 76     @parent
 77   end
 78
 79   def find_project_from_parent_belonging_project
 80     @project = @parent.belonging_project
 81     unless current_user.projects.include?(@project)
 82       redirect_to projects_path
 83       flash[:alert] = msg_page_not_found
 84     end
 85   end

As you can see, I can find the parent and all the variables needed. Now this is how my RSpec controller test for the create action looks like: They all pass:

104         it "can create discussion on project using valid attributes" do                                                                   
105           lambda do
106             post :create, :project_id => project,         
107                           :discussion => valid_attributes
108             flash[:notice].should == 'Discussion started'
109           end.should change(Discussion, :count).by(1)                                                                                     
110         end
111   
112         it "can create discussion on task using valid attributes" do                                                                      
113           lambda do
114             post :create, :task_id => task,               
115                           :discussion => valid_attributes
116             flash[:notice].should == 'Discussion started'
117           end.should change(Discussion, :count).by(1)                                                                                     
118         end
119 
120         it "can create discussion on subtask using valid attributes" do                                                                   
121           lambda do
122             post :create, :subtask_id => subtask,         
123                           :discussion => valid_attributes
124             flash[:notice].should == 'Discussion started'
125           end.should change(Discussion, :count).by(1)                                                                                     
126         end

But, I’d prefer to test creation of discussion on all parents as DRY as possible, something along the lines of the next test.

104         ['project', 'task', 'subtask'].each do |parent|
105           it "can create discussion on #{parent} using valid attributes" do
106             lambda do      
107               post :create, :parent_id => parent,
108                             :discussion => valid_attributes 
109               flash[:notice].should == 'Discussion started'
110             end.should change(Discussion, :count).by(1)
111           end
112         end

How to write this test properly?

I am using FactoryGirl btw. This test above returns > NameError: uninitialized constant Parent

Also, if there is better way/practice of doing this, by all means, correct me 🙂

EDIT: I have solved the problem using the accepted answer below and have stumbled upon another one, very similar, that I again don’t know…

142           it "can access show action for discussion for #{parent}" do
143             get :show, :"#{parent}_id" => self.send(parent),
144                        :id => "disucussion_by_another_user_for_#{parent}"    
146             response.should be_successful   
147           end

This :id parameter is written wrong… Can you please help me write it properly? (this discussion_by_another_user_for_#{parent} are 3 different factories I defined…)

This is how my factories look like:

16   let!(:discussion_by_another_user_for_project) { FactoryGirl.create(:discussion,
 17                                                                      :user => another_user,
 18                                                                      :discussionable => project) }
 19   let!(:discussion_by_another_user_for_task) { FactoryGirl.create(:discussion,
 20                                                                      :user => another_user,
 21                                                                      :discussionable => task) }
 22   let!(:discussion_by_another_user_for_subtask) { FactoryGirl.create(:discussion,
 23                                                                      :user => another_user,
 24                                                                      :discussionable => subtask) }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T16:31:51+00:00Added an answer on June 6, 2026 at 4:31 pm

    I think you were almost there:

    ["project", "task", "subtask'].each do |parent|
      it "can create discussion on #{parent} using valid attributes" do                                                                      
        lambda do   
          post :create, :"#{parent}_id" => self.send(parent),               
                        :discussion => valid_attributes
          flash[:notice].should == 'Discussion started'
        end.should change(Discussion, :count).by(1)                                                                                     
      end
    end
    

    Hope this helps.

    [EDIT] Your extra question: I am not quite sure how you believe handing down a mere string would actually create an object. You create an object with FactoryGirl as follows:

    FactoryGirl.create(:factory_name)
    

    So, if you take that into account, your test becomes:

    it "can access show action for discussion for #{parent}" do
      discussion_by_another_user = FactoryGirl.create("discussion_by_another_user_for_#{parent}".to_sym)    
      get :show, :"#{parent}_id" => self.send(parent),
          :id => discussion_by_another_user.id
      response.should be_successful   
    end
    

    Of course, you could inline the parameter discussion_by_another_user, but I think this is a bit more readable. Hope this helps.

    [EDIT] After clarified question: this is actually completely identical that what you asked before.
    You build a string, but the string is actually a method you want to call, and as before, you
    just write:

    it "can access show action for discussion for #{parent}" do
      get :show, :"#{parent}_id" => self.send(parent),
                 :id => self.send("discussion_by_another_user_for_#{parent}")
      response.should be_successful   
    end
    

    HTH

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a polymorphic resource Discussion that can belong to either the Project, Task,
Let's suppose I have a Discussion model, and that a discussion can be tagged.
I have project model that has many tasks. Both project and tasks can have
I have Discussions (polymorphic) that belong to Projects, Tasks and Subtasks. Tasks belong to
Inspired by the discussion in this question . We have all been taught that
I have a discussion board, and users can post comments on the board. But
I have a .NET class (for discussion, ClassA) that calls a SQL Server stored
I am writing a discussion board software that will have avatar images for the
I have a Sharepoint discussion list that belongs to an Exchange list with the
I have a discussion board with the following table structure: +-------------------------------+ | TABLE: MESSAGES

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.