# Problem using build with has\_many :through relationship

**URL:** <https://www.ruby-forum.com/t/problem-using-build-with-has-many-through-relationship/181274>\
**Category:** Rails\
**Created:** [January 8, 2010, 4:48pm UTC](https://www.ruby-forum.com/t/problem-using-build-with-has-many-through-relationship/181274 "2010-01-08T16:48:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 8, 2010, 4:48pm UTC](https://www.ruby-forum.com/t/problem-using-build-with-has-many-through-relationship/181274/1 "2010-01-08T16:48:09Z")

</div>

Hello all,  
I’m having problems creating a new record with has\_many :through  
My application is quite complicated so I’ll try to explain it as easy as  
I can.

I have a many to many relationship with Projects, IRBs and Reviews.  
Where reviews is the joined model.

In the new project view, I have a link (add IRB). This will add an a  
list of IRBs to the new project form.  
I have visited a Railscast(#73-75) similar to this.  
The difference is I need to search for an IRB# first.  
Therefore when use the link it will open a jQuery UI dialog box. Within  
the dialog, I have a search bar for ‘irbs’.  
When a search is implemented, the results appear in the dialog box. Each  
result has a 'irb#, title and a link “add IRB”  
This link will add/create the association in the Reviews model.

I have setup everything up similar to a Railscast (163. Self-Referential  
Association) but when I click the ‘Add IRB’, I get a “undefined method  
`reviews’ for #Array:0x10322cbb8” error.

My project, irb and review models.

- project model -  
class Project \< ActiveRecord::Base  
has\_many :reviews  
has\_many :irbs, :through =\> :reviews  
end

-irb model -  
class Irb \< ActiveRecord::Base

# Relationships

has\_many :reviews  
has\_many :projects, :through =\> :reviews  
end

- review model -  
class Review \< ActiveRecord::Base  
belongs\_to :irb # foreign key - irb\_id  
belongs\_to :project # foreign key - project\_id  
end

- reviews\_controller -  
class ReviewsController \< ApplicationController  
def create  
@review = Project.reviews.build( :irb\_id =\> params[:id] )  
if @review.save  
flash[:notice] = “IRB added.”  
redirect\_to new\_project\_path  
else  
flash[:error] = “Unable to add IRB.”  
redirect\_to new\_project\_path  
end  
end

end

Here’s the partial to which the “Add IRB” link is.

- \_searchresults.html.erb -  
\<% content\_for :head do %\>  
\<%= stylesheet\_link\_tag ‘dialog’ %\>  
\<% end %\>

 \<% @irbs.each do |irb| %\> \<% end %\>

| IRB Search Results |
| --- |
| IRB ID | Title | PI Full Name | Actions |
| --- | --- | --- | --- |
| \<%=h irb.IRB %\> | \<%=h truncate(irb.Title, :length =\> 30) %\> | \<%=h irb.PI\_Full\_Name %\> | \<%= link\_to 'Add IRB', reviews\_path, :id =\> irb.id, :method =\> :post %\> |

\<%= will\_paginate @irbs %\>

Thank you for any advice or help with this.

John

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [January 8, 2010, 6:04pm UTC](https://www.ruby-forum.com/t/problem-using-build-with-has-many-through-relationship/181274/2 "2010-01-08T18:04:02Z")

</div>

On Jan 8, 3:48Â pm, John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Â def create  
> Â Â @review = Project.reviews.build( :irb\_id =\> params[:id] )

this and

> Â def destroy  
> Â Â @review = Project.reviews.find(params[:id])

this look weird - Project does not have a reviews method - instances  
of it do. Especially in the first case you want to be getting an  
appropriate instance of project first and then do reviews.build on it

Fred
