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