# Handling two models in one form

**URL:** <https://www.ruby-forum.com/t/handling-two-models-in-one-form/127227>\
**Category:** Rails\
**Created:** [January 26, 2008, 4:24pm UTC](https://www.ruby-forum.com/t/handling-two-models-in-one-form/127227 "2008-01-26T16:24:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zoopzoop](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/59ef9b/32.png) [@zoopzoop](https://www.ruby-forum.com/u/zoopzoop)\
**Post date:** [January 26, 2008, 4:24pm UTC](https://www.ruby-forum.com/t/handling-two-models-in-one-form/127227/1 "2008-01-26T16:24:46Z")

</div>

I am just beginning with RoR and ran into my first major headscratcher.

I have a model “events” which is through “has\_and\_belongs\_to\_many”  
(habtm) connected to a model “event\_types”, meaning one event can have  
several event\_types and one event\_type can belong to several events.  
First question: is there a better way to solve this relationship?

Alright, now when creating a new event I want to show the list of  
existing event types and let the user choose one or more for the new  
event through checkboxes.

I fiddled around with how to show the text boxes.  
I am using “form\_for :event” so I tried to use “form.check\_box” but I  
couldn’t get it right.

I got it (halfway) working with the following but it is not pretty:

\<% @event\_types.each do |event\_type| %\>  
\<%= check\_box\_tag “selected\_event\_types[]”, event\_type.id,  
@event.has\_event\_type\_by\_id?(event\_type.id) %\>  
\<%= event\_type.name %\> |  
\<% end %\>

And in the events\_controller:

def new  
@event = Event.new  
@event\_types = get\_all\_event\_types  
@locations = get\_all\_locations  
end

def create  
@event = Event.new(params[:event])  
params[:selected\_event\_types].each do |event\_type\_id|  
@event.add\_event\_type\_by\_id(event\_type\_id)  
end  
if @event.save  
redirect\_to\_index(“Event was successfully created.”)  
else  
@event\_types = get\_all\_event\_types  
@locations = get\_all\_locations  
render :action =\> :new  
end  
end

It does work, I get a list of the selected event type ids in  
params[:selected\_event\_types] but I am sure there must be a smarter way  
to do this…  
Moreover, every checkbox gets the same id (“selected\_event\_types[]”), so  
my doesn’t work properly…

I am open for all thoughts and ideas!

/Zoop

---

<div class="post-metadata">

**Author:** ![zoopzoop](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/59ef9b/32.png) [@zoopzoop](https://www.ruby-forum.com/u/zoopzoop)\
**Post date:** [January 26, 2008, 6:03pm UTC](https://www.ruby-forum.com/t/handling-two-models-in-one-form/127227/2 "2008-01-26T18:03:56Z")

</div>

Ok, I came a bit further in my quest for the perfect solution…

In the view I replaced my old code by this:

\<% @event\_types.each do |event\_type| %\>  
\<%= check\_box\_tag(“event[event\_types][]”,  
event\_type.id,  
@event.has\_event\_type\_by\_id?(event\_type.id),  
{ :id =\> “event\_event\_types\_#{event\_type.id}” }) %\>  
\<%=  
event\_type.name %\> |  
\<% end %\>

Now I get the event\_types in a subarray of params[:event] into my  
controller.

In the controller I want to be able to save the event directly without  
having to add the selected event types “by hand”. So my create function  
looks like this now:

def create  
@event = Event.new(params[:event])  
if @event.save  
redirect\_to\_index(“Event was successfully created.”)  
else  
@event\_types = get\_all\_event\_types  
@locations = get\_all\_locations  
render :action =\> :new  
end  
end

Thus I simply removed the lines  
params[:selected\_event\_types].each do |event\_type\_id|  
@event.add\_event\_type\_by\_id(event\_type\_id)  
end

The error I get now is “EventType expected, got String” which is  
understandable since the value of the checkboxes is “event\_type.id”.  
Setting the value to “event\_type” doesn’t work since i then get  
“event\_type.to\_s”, the string representation.

What to do?

---

<div class="post-metadata">

**Author:** ![zoopzoop](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/59ef9b/32.png) [@zoopzoop](https://www.ruby-forum.com/u/zoopzoop)\
**Post date:** [January 26, 2008, 6:31pm UTC](https://www.ruby-forum.com/t/handling-two-models-in-one-form/127227/3 "2008-01-26T18:31:05Z")

</div>

I found the solution here →   
[http://wiki.rubyonrails.org/rails/pages/HowtoUseFormOptionHelpers](http://wiki.rubyonrails.org/rails/pages/HowtoUseFormOptionHelpers)  
Look under “Assigning to has\_many or has\_and\_belongs\_to\_many  
collections”

Changed my code to

[…]  
\<%= check\_box\_tag(“event[event\_type\_ids][]”,  
[…]

and now it works fine!
