# Updating HABTM records is easy, but why isnâ€™t one\_to\_many?

**URL:** <https://www.ruby-forum.com/t/updating-habtm-records-is-easy-but-why-isna-t-one-to-many/123351>\
**Category:** Rails\
**Created:** [December 16, 2007, 9:39pm UTC](https://www.ruby-forum.com/t/updating-habtm-records-is-easy-but-why-isna-t-one-to-many/123351 "2007-12-16T21:39:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![adrie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@adrie](https://www.ruby-forum.com/u/adrie)\
**Post date:** [December 16, 2007, 9:39pm UTC](https://www.ruby-forum.com/t/updating-habtm-records-is-easy-but-why-isna-t-one-to-many/123351/1 "2007-12-16T21:39:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![adrie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@adrie](https://www.ruby-forum.com/u/adrie)\
**Post date:** [December 16, 2007, 11:41pm UTC](https://www.ruby-forum.com/t/updating-habtm-records-is-easy-but-why-isna-t-one-to-many/123351/2 "2007-12-16T23:41:05Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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/](http://www.ruby-forum.com/).
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

---

<div class="post-metadata">

**Author:** ![adrie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@adrie](https://www.ruby-forum.com/u/adrie)\
**Post date:** [December 18, 2007, 12:51pm UTC](https://www.ruby-forum.com/t/updating-habtm-records-is-easy-but-why-isna-t-one-to-many/123351/3 "2007-12-18T12:51:06Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> 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

---

<div class="post-metadata">

**Author:** ![adrie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@adrie](https://www.ruby-forum.com/u/adrie)\
**Post date:** [December 19, 2007, 1:10am UTC](https://www.ruby-forum.com/t/updating-habtm-records-is-easy-but-why-isna-t-one-to-many/123351/4 "2007-12-19T01:10:48Z")

</div>

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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

---

<div class="post-metadata">

**Author:** ![adrie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@adrie](https://www.ruby-forum.com/u/adrie)\
**Post date:** [December 19, 2007, 2:34am UTC](https://www.ruby-forum.com/t/updating-habtm-records-is-easy-but-why-isna-t-one-to-many/123351/5 "2007-12-19T02:34:58Z")

</div>

On 12/18/07, Ryan B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![adrie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@adrie](https://www.ruby-forum.com/u/adrie)\
**Post date:** [December 19, 2007, 2:39am UTC](https://www.ruby-forum.com/t/updating-habtm-records-is-easy-but-why-isna-t-one-to-many/123351/6 "2007-12-19T02:39:39Z")

</div>

My apologies.

---

<div class="post-metadata">

**Author:** ![adrie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@adrie](https://www.ruby-forum.com/u/adrie)\
**Post date:** [December 18, 2007, 10:50pm UTC](https://www.ruby-forum.com/t/updating-habtm-records-is-easy-but-why-isna-t-one-to-many/123351/7 "2007-12-18T22:50:24Z")

</div>

On 12/16/07, Adrie D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://railscasts.com/episodes;archive)

Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![adrie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@adrie](https://www.ruby-forum.com/u/adrie)\
**Post date:** [December 21, 2007, 7:27pm UTC](https://www.ruby-forum.com/t/updating-habtm-records-is-easy-but-why-isna-t-one-to-many/123351/8 "2007-12-21T19:27:34Z")

</div>

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
