# Form question

**URL:** <https://www.ruby-forum.com/t/form-question/104580>\
**Category:** Rails\
**Created:** [May 19, 2007, 10:45pm UTC](https://www.ruby-forum.com/t/form-question/104580 "2007-05-19T22:45:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kelleysislander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kelleysislander](https://www.ruby-forum.com/u/kelleysislander)\
**Post date:** [May 19, 2007, 10:45pm UTC](https://www.ruby-forum.com/t/form-question/104580/1 "2007-05-19T22:45:27Z")

</div>

Suppose I have the following models/migrations:

class Car \< ActiveRecord::Base  
belongs\_to :manufacturer  
validates\_presence\_of :manufacturer  
validates\_association :manufacturer  
end

class CreateCars \< ActiveRecord::Migration  
def self.up  
create\_table cars do |t|  
t.column :name, :string  
t.column :manufacturer\_id, :integer  
end  
end

class Manufacturer \< ActiveRecord::Base  
has\_many :cars  
end

class CreateManufacturers \< ActiveRecord::Migration  
def self.up  
create\_table maufacturers do |t|  
t.column :name, :string  
end  
end

Now, I need a form to create new Cars that has two text fields: name,  
and manufacturer. I can’t use a select for manufacturer, because there  
are far too many of them (let’s pretend). What I can’t figure out is  
how to construct the text\_field helper tag in my form such that Rails  
understands that the form field doesn’t map directly to a field in the  
Car object, but rather to the name field of the car’s Manufacturer  
object. Is there even a way to do it? I’m looking for something like  
one of these (neither of which work, obviously, but they may help to  
clarify what I’m trying to do):

\<%= text\_field ‘car’, ‘car.manufacturer.name’ %\> or  
\<%= text\_field ‘car’, ‘car.manufacturer[:name]’ %\>

Without the ability to do that, I’m not sure how I could have - for  
example - an edit form without writing special code to populate that  
field, or how any of the validates\_\* methods would work. I’ve tried  
using fields\_for, but it doesn’t work; probably because I’m not doing  
it right. Maybe someone knows how to use fields\_for to accomplish the  
above? I think what I tried was something like this:

\<% fields\_for :manufacturer, @car.manufacturer do |m\_field| %\>  
\<%= m\_field.text\_field :name %\>  
\<% end %\>

–  
Bill K.

---

<div class="post-metadata">

**Author:** ![kelleysislander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kelleysislander](https://www.ruby-forum.com/u/kelleysislander)\
**Post date:** [May 20, 2007, 12:44am UTC](https://www.ruby-forum.com/t/form-question/104580/2 "2007-05-20T00:44:35Z")

</div>

One thing you can do is define a pair of methods on the car object  
something like this (off the top of my head).

def manufacturer\_name=(value)  
manufacturer.create(:name =\> value)  
end

def manufacturer\_name  
manufacturer.name  
end

---

<div class="post-metadata">

**Author:** ![kelleysislander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kelleysislander](https://www.ruby-forum.com/u/kelleysislander)\
**Post date:** [May 20, 2007, 1:31am UTC](https://www.ruby-forum.com/t/form-question/104580/3 "2007-05-20T01:31:38Z")

</div>

On May 19, 6:44 pm, dasil003 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> One thing you can do is define a pair of methods on the car object  
> something like this (off the top of my head).
> 
> def manufacturer\_name=(value)  
> manufacturer.create(:name =\> value)  
> end
> 
> def manufacturer\_name  
> manufacturer.name  
> end

Won’t manufacturer.create store a new manufacturer in the  
manufacturers table? In my case, they’re all pre-defined. Users  
populating this form aren’t creating a new one, they’re typing the  
name of one that should already exist, after which I validate.

But on that note, I did try something like this:

def manufacturer=(m)  
if m.instance\_of? String  
self.manufacturer = Manufacturer.find\_by\_name(m)  
else  
self.manufacturer = m  
end  
end

Of course, this resulted in an endless loop. Woops.

-Bill K.

---

<div class="post-metadata">

**Author:** ![kelleysislander](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kelleysislander](https://www.ruby-forum.com/u/kelleysislander)\
**Post date:** [May 20, 2007, 2:57am UTC](https://www.ruby-forum.com/t/form-question/104580/4 "2007-05-20T02:57:42Z")

</div>

On May 19, 7:30 pm, Bill K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > manufacturer.name  
> > end
> 
> Won’t manufacturer.create store a new manufacturer in the  
> manufacturers table? In my case, they’re all pre-defined.

Actually, you’ve pretty much nailed it. I set my form up like:

\<%= text\_field\_for ‘car’, ‘manufacturer\_name’ %\>

And in Car:

def manufacturer\_name=(m)  
self.manufacturer = Manufacturer.find\_by\_name(m)  
end

def manufacturer\_name  
self.manufacturer.name  
end

This seems to do pretty much exactly what I was trying to accomplish,  
and my validates\_presence\_of works. The only thing that doesn’t work  
is validates\_associated :manufacturer, because when an invalid  
manufacturer name is entered it isn’t found in the database, so  
manufacturer remains nil and the validates\_presence\_of fails instead.  
But I can live with that.

Thanks for the brain kick. 🙂

-Bill
