I’ve got an Event and Activity. Each event has one activity, and the user needs to pick from a large number of activities. I’ve got a collection_select field that shows properly in the Event form, but it won’t save. I keep getting a Couldn't find Activity without an ID error.
I thought that by creating the has_one belongs_to association in my models that the forms would automatically know what to do. Nope. So I started adding methods to the Events controller, not even knowing if they are correct. But things didn’t break.
Now I’m thinking that I probably need to create a local variable in the form, but I’ve tried a dozen variations and can’t get anything to work. I just end up getting other error messages.
My Event model
class Event < ActiveRecord::Base
attr_accessible :date, :activity
has_one :activity
belongs_to :creator, :class_name => "User"
has_many :attendees, :class_name => "User"
has_many :comments
end
Activity model
class Activity < ActiveRecord::Base
attr_accessible :location, :title, :image, :image_cache
mount_uploader :image, ImageUploader
belongs_to :event
end
part of Events controller
def new
@event = Event.new
@activities = Activity.find(:all, :order => 'title')
end
def create
@event = Event.new(params[:event])
@activities = Activity.find(:all, :order => 'title')
@activity = Activity.find(params[:id])
end
Events form
<div class="field">
<h6>When do you want to go?</h6><br />
<%= f.text_field :date %>
</div>
<h5>Activities</h5>
<%= collection_select(:activity, :event_id, Activity.all, :id, :title, {:include_blank => 'Please Select'} ) %>
<div class="actions">
<%= f.submit %>
</div>
Wherein lies my ignorance?
When doing this you need to use a nested form structure in order to update both models at the same time. You need to add accepts_nested_attributes_for to your Event model.rb file. Below are what each of your files should look like.
Event Model
Activity Model is good bc you already have the belongs_to event
Event Controller:
Form
If you want a good walk through to look at checkout the railscast on nested model forms
In response to the questions in the comments…yes I would reverse you model relationship setup so you have…
this way each event will have an activity_id column which will identify which activity corresponds with it and is used for rails to make the association.
Now for the many_to_many associations, which it sounds like you will need, you have two options. You can use has_many through or HABTM. The simplest rule of thumb is that you should set up a has_many :through relationship if you think you will need to work with the relationship model as an independent entity. Check out this rails guide for more explanation and details about all the methods these associations will create.
I always like has_many through since you always have the option to work with the relationship if you choose to in the future so here is how you would do that
You can call the relationship whatever you want, “plan” is just what I thought of first.