Updating HABTM records is easy, but why isn’t one_to_many (has_many and
belongs_to)?
With the collection_select it is easy to update HABTM records, everthing
is taken care of, besides declar-ing the variable for all records in de
related tabel. But updating more than one record from a belongs_to table
seems to need a lot of more coding. Or am I missing something?
Models:
class Event < ActiveRecord::Base
has_many :evtimes
end
class Evtime < ActiveRecord::Base
belongs_to :event
end
In EventsController.rb
def edit
@event = Event.find(params[:id])
@evtimes = Evtime.find(:all, :conditions => [“event_id = ?”,
@event.id])
end
def update
@event = Event.find(params[:id])
@evtimes = Evtime.find(params[:evtimes])
@event << @evtimes
if @event.update_attributes(params[:event])
flash[:notice] = ‘Event was successfully updated.’
redirect_to :action => ‘show’, :id => @event
else
render :action => ‘edit’
end
end
And in _form.rhtml
<%= error_messages_for ‘event’ %>
Name
<%= text_field 'event', 'name' %>
City
<%= text_field 'event', 'city' %>
Description
<%= text_field 'event', 'description' %>
<% for evtime in @evtimes do %> Starting :<%= time_select 'evtime', 'starting' %>
Ending :<%= time_select 'evtime', 'ending' %>
Action: <%= text_field 'evtime', 'movement'%>
Notes:
<%= text_area 'evtime', 'notes' %>
<% end %>
I’ve tried a lot of combinations, but nothing went the right way.
Please help, I’m starting to become a Rubyist, but this kind of problems
keep diappointing me.
Thanks,
Adrie