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
--
Ron Miles
ronald.miles@gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://wrath.rubyonrails.org/pipermail/rails/attachments/20060417/aee7dc9d/attachment-0001.html
Ron Miles wrote:> class Resource < ActiveRecord::Base > has_one :other_thing > end > > class OtherThing < ActiveRecord::Base > belongs_to :resource > end > > select(:resource.other_thing, :field1, @options), > select(:resource, :other_thing.field1, @options), > 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 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.
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 On 4/17/06, Mark Reginald James <mrj@bigpond.net.au> wrote:> > Ron Miles wrote: > > > class Resource < ActiveRecord::Base > > has_one :other_thing > > end > > > > class OtherThing < ActiveRecord::Base > > belongs_to :resource > > end > > > > select(:resource.other_thing, :field1, @options), > > select(:resource, :other_thing.field1, @options), > > 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 > > 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. > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Ron Miles ronald.miles@gmail.com 413.329.4277 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060418/4c6fa27a/attachment.html
Maybe Matching Threads
- ActiveRecord belongs_to ActiveResource
- [NQ] How to set an initial FK for a new item of something?
- has_many + finder_sql & :include
- rspec model testing - test on user defined validation- How do I test that the create failed.
- I need "validates_presence_of" help