# Two Models one form

**URL:** <https://www.ruby-forum.com/t/two-models-one-form/59610>\
**Category:** Rails\
**Created:** [May 4, 2006, 5:20pm UTC](https://www.ruby-forum.com/t/two-models-one-form/59610 "2006-05-04T17:20:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![subimage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@subimage](https://www.ruby-forum.com/u/subimage)\
**Post date:** [May 4, 2006, 5:20pm UTC](https://www.ruby-forum.com/t/two-models-one-form/59610/1 "2006-05-04T17:20:41Z")

</div>

I have setup a basic conference registration form. Within that form I  
have an optional piece of information that would only be filled out if  
the registrant was a vendor. Here is my relationship:

Registration has\_one :vendor  
Vendor belongs\_to :registration

I want to be able to send both the vendor information and registration  
information with the same form, but the vendor information might not be  
sent if the registrant wasn’t a vendor. I have my fields set up for the  
vendor but I don’t think they are right:

\<%= select(“vendor”, “donation”, [[‘Door Prize’, ‘doorprize’],  
[‘Monetary donation for door prize’, ‘monetary’], [‘Sponsor food  
eventâ??breakfast ($700)’, ‘breakfast’], [‘Sponsor food eventâ??snack  
($300)’, ‘snack’], [‘Sponsor food eventâ??dinner ($3000)’, ‘dinner’],  
[‘Sponsor entertainment at dinner ($650)’, ‘entertainment’] ],  
{:include\_blank =\> true})%\>

\<%= select(“vendor”, “monetary\_donation”, [[’$100’, ‘100’], [’$200’,  
‘200’], [’$300’, ‘300’], [’$400’, ‘400’], [’$500’, ‘500’], [’$600’,  
‘600’] ], {:include\_blank =\> true})%\>

\<%= text\_field ‘vendor’, ‘other’ %\>

\<%= check\_box “vendor”, “electricity” %\>

I get the error “Vendor expected, got String.” Do I need to manually  
insert the vendor data in the controller? Like this:

@reg = Registration.new(params[:registration])  
@reg.vendor.donation = params[:vendor\_donation]  
@reg.vendor.monetary\_donation = params[:vendor\_monetary\_donation]  
@reg.vendor.other = params[:vendor\_other]  
@reg.vendor.electricity = params[:vendor\_electricity]

Or is there an easier way?

---

<div class="post-metadata">

**Author:** ![subimage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@subimage](https://www.ruby-forum.com/u/subimage)\
**Post date:** [May 4, 2006, 6:20pm UTC](https://www.ruby-forum.com/t/two-models-one-form/59610/2 "2006-05-04T18:20:11Z")

</div>

I could be wrong but I think you need to initialize a vendor. i.e:

@reg = Registration.new(params[:registration])  
@reg.vendor = Vendor.new  
@reg.vendor.donation =  
etc…

-Adam

---

<div class="post-metadata">

**Author:** ![subimage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@subimage](https://www.ruby-forum.com/u/subimage)\
**Post date:** [May 4, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/two-models-one-form/59610/3 "2006-05-04T18:21:11Z")

</div>

Oh, and for your other question you can do:

@reg.vendor = Vendor.new(params[:vendor])

and so long as your params hash is filled, that’ll work fine.

---

<div class="post-metadata">

**Author:** ![subimage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@subimage](https://www.ruby-forum.com/u/subimage)\
**Post date:** [May 4, 2006, 8:48pm UTC](https://www.ruby-forum.com/t/two-models-one-form/59610/4 "2006-05-04T20:48:28Z")

</div>

> Does this relationship assume I have a registration\_id field in my  
> vendors table?
> 
> I do and I think that is right but I can’t remember.

Yep.

-Adam

---

<div class="post-metadata">

**Author:** ![subimage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@subimage](https://www.ruby-forum.com/u/subimage)\
**Post date:** [May 4, 2006, 7:02pm UTC](https://www.ruby-forum.com/t/two-models-one-form/59610/5 "2006-05-04T19:02:36Z")

</div>

Adam B. wrote:

> Oh, and for your other question you can do:
> 
> @reg.vendor = Vendor.new(params[:vendor])
> 
> and so long as your params hash is filled, that’ll work fine.

Does this relationship assume I have a registration\_id field in my  
vendors table?

I do and I think that is right but I can’t remember.

---

<div class="post-metadata">

**Author:** ![subimage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@subimage](https://www.ruby-forum.com/u/subimage)\
**Post date:** [May 5, 2006, 4:52am UTC](https://www.ruby-forum.com/t/two-models-one-form/59610/6 "2006-05-05T04:52:54Z")

</div>

> Will this not allow me to do validation on _both_ models? It isn’t  
> picking up the validation on one of them.
> 
> Seth

I do believe that is because I am dumb. This is probably what you should  
do:

@reg = Registration.new(params[:registration])  
vend = Vendor.new(params[:vendor])  
if vendor.save  
@reg.vendor = vend  
if @reg.save  
  
else  
  
end  
else  
\<same as the else clause of the if @reg.save I believe\>  
end

Validation actually happens on save.

-Adam

---

<div class="post-metadata">

**Author:** ![subimage](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/aca169/32.png) [@subimage](https://www.ruby-forum.com/u/subimage)\
**Post date:** [May 4, 2006, 10:18pm UTC](https://www.ruby-forum.com/t/two-models-one-form/59610/7 "2006-05-04T22:18:52Z")

</div>

Adam B. wrote:

> > Does this relationship assume I have a registration\_id field in my  
> > vendors table?
> > 
> > I do and I think that is right but I can’t remember.
> 
> Yep.
> 
> -Adam

Will this not allow me to do validation on _both_ models? It isn’t  
picking up the validation on one of them.

Seth
