Updating HABTM records is easy, but why isn’t one_to_many?

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

What is evtime? You didn’t explain this at all in your code.

Is it event time? Why is it being stored in a separate table?

On Dec 17, 2007 7:09 AM, Adrie D. [email protected]
wrote:

class Event < ActiveRecord::Base
@evtimes = Evtime.find(:all, :conditions => [“event_id = ?”,
else

Action: <%= text_field ‘evtime’, ‘movement’%>

Posted via http://www.ruby-forum.com/.


Ryan B.

Ryan B. wrote:

What is evtime? You didn’t explain this at all in your code.

Is it event time? Why is it being stored in a separate table?

On Dec 17, 2007 7:09 AM, Adrie D. [email protected]
wrote:

class Event < ActiveRecord::Base
@evtimes = Evtime.find(:all, :conditions => [“event_id = ?”,
else

Action: <%= text_field ‘evtime’, ‘movement’%>

Ryan, thanks for your reply,

Evtime is short for eventtimes. I want to be able to update
eventtimes(Evtime records) on the same page as the related Event (Event
record). My problem is (was?) that i can’t get the event.evtimes
updateable. But i haven’t stop searching and found this. I did not yet
try it.

<% @event.evtimes.each do |@evtime| %>
Starting :<%= time_select “evtime[]”, “evtime_starting” %>

Ending :<%= time_select “evtime[]”, “evtime_ending” %>

Action: <%= text_field “evtime[]”, “evtime_movement” %>

Notes:
<%= text_area “evtime[]”, “evtime_notes” %>

<% end %>

Adrie

my question is that why would you have multiple event times? Why not
just
store the time/date of the event on the event itself?

On Dec 19, 2007 8:19 AM, Rick DeNatale [email protected] wrote:

You’re working too hard.

class Evtime < ActiveRecord::Base
@event = Event.find(params[:id])
And in _form.rhtml
<%= text_field ‘event’, ‘description’ %>


There are better ways to do the view, e.g. using the form_for helper


Ryan B.

On 12/18/07, Ryan B. [email protected] wrote:

my question is that why would you have multiple event times? Why not just
store the time/date of the event on the event itself?

Who is to say that his Events are one-time only. He wasn’t asking
about a domain question.

It would be nice, by the way if you stopped top-posting.

A. Because it makes the conversation hard to follow.

Q. Why shouldn’t you top post?


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

My apologies.

On 12/16/07, Adrie D. [email protected] wrote:

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?

You’re working too hard.

Models:
class Event < ActiveRecord::Base
has_many :evtimes
end

The has many lets you get at and manipulate the list of Evtimes from the
Event.

class Evtime < ActiveRecord::Base
belongs_to :event
end

In EventsController.rb
def edit
@event = Event.find(params[:id])
@evtimes = Event.evtimes
end

def update
@event = Event.find(params[:id])
@event.evtimes << Evtime.find(params[:evtimes])


<% 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 %>

There are better ways to do the view, e.g. using the form_for helper
and using a partial for the Evtimes list, but that’s another story.
You also need to handle the fact that you are editing multiple
Evtimes. For that I’d direct you to RailsCasts episodes 73-75
http://railscasts.com/episodes;archive

Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Rick,

Very much thanks, that’s the code i was looking for.
It’s still not easy to switch to Ruby with a 25 year RPG-background.

Thanks and have a beautiful Christmas,
Adrie D.
Netherlands