I have two models Events and Interests which are connected by a join table migration EventsInterests (event_id, interest_id, id=>false).
I was trying to create something where you could connect a set of interest categories to some events within my app.
I created a record in directly in the database and its showing up correctly, so I'm pretty sure the checkbox part is correct; but I cant edit the the record at all. If I add another interest through the checkbox, nothing happens..Im not getting any errors and the log shows it calling the join table, so I'm guessing the issue is with my update statement.....
Im thinking its something with attr_accessible part but I dont know how to add that for a join table....do i just delete it?
attr_accessible :name, :category
In my edit form where I would like to add the records to the join table it looks like this.
<%= form_for @event do |f| %>
<%= f.error_messages %>
<%= f.label :title %><br />
<%= f.text_field :title %>
<% for interest in @interests %>
<%= check_box_tag "event[interest_ids]", interest.id, @event.interests.include?(interest) %>
<%= interest.name %>
<% end %>
<p><%= f.submit %></p>
and the edit/update part in the controller looks like
@event = Event.find(params[:id])
params[:event][:interest_ids] ||= 
flash[:notice] = "Successfully updated event."
render :action => 'edit'
If you want to update such models, you should use a has_many :through relation. Has and belongs to models make only sense when only creating / deleting a relation..