Forum: Ruby on Rails Two Models one form

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Seth B. (Guest)
on 2006-05-04 19:20
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?
Adam B. (Guest)
on 2006-05-04 20:20
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
Adam B. (Guest)
on 2006-05-04 20:21
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.
Seth B. (Guest)
on 2006-05-04 21:02
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.
Adam B. (Guest)
on 2006-05-04 22:48
 > 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
Seth B. (Guest)
on 2006-05-05 00:18
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
Adam B. (Guest)
on 2006-05-05 06:52
> 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
      <normal stuff>
   else
      <normal stuff>
   end
else
   <same as the else clause of the if @reg.save I believe>
end

Validation actually happens on save.

-Adam
This topic is locked and can not be replied to.