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 def destroy @review = Project.reviews.find(params[:id]) @review.destroy flash[:notice] = "IRB Removed." redirect_to new_project_path 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 %> <table class="searchResults"> <tr><th colspan="7">IRB Search Results</th></tr> <tr> <th>IRB ID</th> <th>Title</th> <th>PI Full Name</th> <th>Actions</th> </tr> <% @irbs.each do |irb| %> <tr id=''<%= irb.id %>''> <td><%=h irb.IRB %></td> <td><%=h truncate(irb.Title, :length => 30) %></td> <td><%=h irb.PI_Full_Name %></td> <td><%= link_to ''Add IRB'', reviews_path, :id => irb.id, :method => :post %></td> </tr> <% end %> </table> <%= will_paginate @irbs %> Thank you for any advice or help with this. John -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-Jan-08 17:02 UTC
Re: problem using build with has_many :through relationship
On Jan 8, 3:48 pm, John Mcleod <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.