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?
events.rb
has_and_belongs_to_many :interests
attr_accessible :name, :category
interests.rb
has_and_belongs_to_many :events
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 %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<% for interest in @interests %>
<div>
<%= check_box_tag "event[interest_ids][]", interest.id, @event.interests.include?(interest) %>
<%= interest.name %>
</div>
<% end %>
<p><%= f.submit %></p>
and the edit/update part in the controller looks like
def update
@event = Event.find(params[:id])
params[:event][:interest_ids] ||= []
if @event.update_attributes(params[:event])
flash[:notice] = "Successfully updated event."
redirect_to @event
else
render :action => 'edit'
end
end