# Has\_one problem in a form

**URL:** <https://www.ruby-forum.com/t/has-one-problem-in-a-form/57960>\
**Category:** Rails\
**Created:** [April 17, 2006, 2:52am UTC](https://www.ruby-forum.com/t/has-one-problem-in-a-form/57960 "2006-04-17T02:52:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rawn027](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d07c76/32.png) [@rawn027](https://www.ruby-forum.com/u/rawn027)\
**Post date:** [April 17, 2006, 2:52am UTC](https://www.ruby-forum.com/t/has-one-problem-in-a-form/57960/1 "2006-04-17T02:52:32Z")

</div>

Hello all,

I have a syntax question. How does one refer to a field brought in via  
a  
has\_one/belongs\_to relationship in a form helper? Basically, I have two  
models:

class Resource \< ActiveRecord::Base  
has\_one :other\_thing  
end

class OtherThing \< ActiveRecord::Base  
belongs\_to :resource  
end

It has the normal id-resource\_id foreign key relationship. OtherThing  
has  
two fields, plus the foreign key, call them field1 and field2.

In the ruby console, I can access field1 a couple of ways:

resource.other\_thing.field1  
resource.other\_thing[‘field1’]

Maybe a few other ways.

What I want is to update field1 in a form that also updates fields in  
the  
resources table. It’s simple for fields that REALLY belong to resource,  
but  
seemingly a problem for related fields in other tables.

select(:resource, :status, @statuses) works fine, for instance.

I’m looking for

select(:resource.other\_thing, :field1, @options),  
select(:resource, :other\_thing.field1, @options),  
select(‘resource’, ‘other\_thing.field1’, @options),  
select(:resource, :other\_thing[:field1], @options)  
or something to like that, but no luck.

Anyone have any thoughts on the proper syntax?

Thanks in advance,

-Ron

---

<div class="post-metadata">

**Author:** ![rawn027](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d07c76/32.png) [@rawn027](https://www.ruby-forum.com/u/rawn027)\
**Post date:** [April 17, 2006, 7:15am UTC](https://www.ruby-forum.com/t/has-one-problem-in-a-form/57960/2 "2006-04-17T07:15:15Z")

</div>

Ron M. wrote:

> select(‘resource’, ‘other\_thing.field1’, @options),  
> select(:resource, :other\_thing[:field1], @options)

No, you can’t do that at present, though a long time ago I  
wrote a patch for it: [http://dev.rubyonrails.org/ticket/2053](http://dev.rubyonrails.org/ticket/2053)

Instead you need to write:

@other\_thing = @resource.other\_thing  
select :other\_thing, :field1

then process their posted parameters separately in your  
controller.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![rawn027](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/d07c76/32.png) [@rawn027](https://www.ruby-forum.com/u/rawn027)\
**Post date:** [April 18, 2006, 2:32pm UTC](https://www.ruby-forum.com/t/has-one-problem-in-a-form/57960/3 "2006-04-18T14:32:39Z")

</div>

Thanks Mark,

The select() loads properly with the option data now, but it doesn’t  
reflect  
the exising value in the database for some reason. I was able to bypass  
that by using if/else logic in the form view to mark the “selected”  
item.  
Thanks for the push in the right direction (separating out other\_thing  
into  
it’s own variable). Much appreciated,

-Ron
