# Accessing belongs\_to objects from a form\_for context

**URL:** <https://www.ruby-forum.com/t/accessing-belongs-to-objects-from-a-form-for-context/102138>\
**Category:** Rails\
**Created:** [August 11, 2006, 2:24pm UTC](https://www.ruby-forum.com/t/accessing-belongs-to-objects-from-a-form-for-context/102138 "2006-08-11T14:24:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dissolved](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b3f665/32.png) [@dissolved](https://www.ruby-forum.com/u/dissolved)\
**Post date:** [August 11, 2006, 2:24pm UTC](https://www.ruby-forum.com/t/accessing-belongs-to-objects-from-a-form-for-context/102138/1 "2006-08-11T14:24:14Z")

</div>

In a form\_for context, is there a way to access objects that are related  
to the primary object with a belongs\_to? I think an example will serve  
best:

class User \< ActiveRecord::Base  
belongs\_to :person  
attr\_accessor :username  
end

class Person \< ActiveRecord::Base  
has\_one :user  
attr\_accessor :first\_name  
end

\<% form\_for :user do |form| %\>  
\<%= form.text\_field :username, :size =\> 40%\>  
\<%= form.text\_field “person.first\_name”, :size =\> 40%\> ???  
doesn’t work  
\<%= submit\_tag “Add User”, :class =\> “submit” %\>  
\<% end %\>

Is there a way to do this?

---

<div class="post-metadata">

**Author:** ![dissolved](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b3f665/32.png) [@dissolved](https://www.ruby-forum.com/u/dissolved)\
**Post date:** [August 22, 2006, 5:16am UTC](https://www.ruby-forum.com/t/accessing-belongs-to-objects-from-a-form-for-context/102138/2 "2006-08-22T05:16:08Z")

</div>

Apparently this stumped the entire community. I did find (on  
[http://news.gmane.org/gmane.comp.lang.ruby.rails](http://news.gmane.org/gmane.comp.lang.ruby.rails)) that I was not alone  
in trying to do something like this, but it appears not to be possible  
in the way I was trying to do it. For the benefit of others who may get  
stuck on this, here is what I’ve got now:

#MODELS  
class User \< ActiveRecord::Base  
belongs\_to :person  
end

class Person \< ActiveRecord::Base  
has\_one :user  
end

#VIEW  
\<%= form\_tag :action =\> :signup %\>  
\<%= text\_field :user, :username, :size =\> 40%\>

# … more :user fields …

\<%= text\_field :person, :first\_name, :size =\> 40%\>

# … more :person fields …

\<%= submit\_tag “Add User” %\>  
\<%= end\_form\_tag %\>

#CONTROLLER

def signup  
@user = User.new(params[:user])  
@person = @user.build\_person(params[:person])  
if request.post? && @user.save  
flash[:notice] = “User #{@user.username} created.”  
redirect\_to(:action =\> “login”)  
end  
end

IN SUMMARY  
I learned that I shouldn’t have the attr\_accessor’s for database  
backed attributes in the model. I learned that there is currently no  
way to access child objects through the form\_for mechanism (use form\_tag  
instead). The code in the controller seems to tie it all up nicely,  
although I’ve yet to figure out how to handle correctly failed  
validations in the person object. My code will save the user, and  
silently not save the person if validation on the person fails.

Ryan

---

<div class="post-metadata">

**Author:** ![dissolved](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b3f665/32.png) [@dissolved](https://www.ruby-forum.com/u/dissolved)\
**Post date:** [August 22, 2006, 5:42am UTC](https://www.ruby-forum.com/t/accessing-belongs-to-objects-from-a-form-for-context/102138/3 "2006-08-22T05:42:31Z")

</div>

Okay, that last problem was an easy find… adding a call to  
validates\_associated to the user model as follows does the trick:

class User \< ActiveRecord::Base  
belongs\_to :person  
validates\_associated :person  
end

---

<div class="post-metadata">

**Author:** ![dissolved](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b3f665/32.png) [@dissolved](https://www.ruby-forum.com/u/dissolved)\
**Post date:** [August 12, 2006, 3:56am UTC](https://www.ruby-forum.com/t/accessing-belongs-to-objects-from-a-form-for-context/102138/4 "2006-08-12T03:56:26Z")

</div>

Still trying to figure out similar issues myself…

did you try “person[first\_name]” ?

Ryan Sandridge wrote:

> In a form\_for context, is there a way to access objects that are related  
> to the primary object with a belongs\_to? I think an example will serve  
> best:
> 
> class User \< ActiveRecord::Base  
> belongs\_to :person  
> attr\_accessor :username  
> end
> 
> class Person \< ActiveRecord::Base  
> has\_one :user  
> attr\_accessor :first\_name  
> end
> 
> \<% form\_for :user do |form| %\>  
> \<%= form.text\_field :username, :size =\> 40%\>  
> \<%= form.text\_field “person.first\_name”, :size =\> 40%\> ???  
> doesn’t work  
> \<%= submit\_tag “Add User”, :class =\> “submit” %\>  
> \<% end %\>
> 
> Is there a way to do this?

---

<div class="post-metadata">

**Author:** ![dissolved](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b3f665/32.png) [@dissolved](https://www.ruby-forum.com/u/dissolved)\
**Post date:** [August 22, 2006, 6:27am UTC](https://www.ruby-forum.com/t/accessing-belongs-to-objects-from-a-form-for-context/102138/5 "2006-08-22T06:27:08Z")

</div>

On Tuesday 22 August 2006 05:16, Ryan Sandridge wrote:

> Â I learned that I shouldn’t have the attr\_accessor’s for database  
> backed attributes in the model. Â I learned that there is currently no  
> way to access child objects through the form\_for mechanism (use  
> form\_tag instead).

Have look at the docs for fields\_for

> **[ActionView::Helpers::FormHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#M000388)**
>
> Form helpers are designed to make working with resources much easier compared to using vanilla HTML.

It looks like that’s what you need.

Michael

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)

---

<div class="post-metadata">

**Author:** ![dissolved](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b3f665/32.png) [@dissolved](https://www.ruby-forum.com/u/dissolved)\
**Post date:** [August 23, 2006, 3:00am UTC](https://www.ruby-forum.com/t/accessing-belongs-to-objects-from-a-form-for-context/102138/6 "2006-08-23T03:00:54Z")

</div>

Michael S. wrote:

> On Tuesday 22 August 2006 05:16, Ryan Sandridge wrote:
> 
> > I learned that I shouldn’t have the attr\_accessor’s for database  
> > backed attributes in the model. I learned that there is currently no  
> > way to access child objects through the form\_for mechanism (use  
> > form\_tag instead).
> 
> Have look at the docs for fields\_for
> 
> [ActionView::Helpers::FormHelper](http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#M000388)
> 
> It looks like that’s what you need.
> 
> Michael

Very nice. It does indeed look like it could do the trick. Considering  
how often I saw this question asked on other rails forums, I’m shocked  
to just now see this answer.

Thanks much,  
Ryan
