Reputation: 7495
Hi I'm having trouble defining this association as a has_many which I would like to do in order to get accepts_nested_attributes_for to work.
def work_days
work_days = WorkDay.user_id_equals(self.user_id).company_id_equals(self.company_id)
end
accepts_nested_attributes_for :work_days
What would be a good solution? Is there anyway I can just say that this is an association?
-- Rails 3.0.7
Upvotes: 0
Views: 81
Reputation: 107738
Firstly I would write the work_days
method like this:
def work_days
WorkDay.where(:user_id => user_id, :company_id => company_id)
end
Next, accepts_nested_attributes_for
is really only for real associations, and so I would define a method like this:
def work_days=(work_day_params)
# iterate over work_day_params and define new work day objects as required
end
(I may have the method name wrong)
In your form you would have something like this:
<% f.fields_for :work_days do |work_day| %>
<%= render :partial => "work_days/fields", :locals => { :f => work_day } %>
<% end %>
In that partial you would have whatever is necessary for a work day. Once this form is submitted the params should be sent through to the controller as params[:nest][:work_days]
and with the work_days=
method defined on your model, it should accept them and create them just like accepts_nested_attributes_for
does.
Upvotes: 1