# Nested resources and scoped ActiveRecord calls

**URL:** <https://www.ruby-forum.com/t/nested-resources-and-scoped-activerecord-calls/130280>\
**Category:** Rails\
**Created:** [February 27, 2008, 1:06am UTC](https://www.ruby-forum.com/t/nested-resources-and-scoped-activerecord-calls/130280 "2008-02-27T01:06:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bryan82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a87d85/32.png) [@bryan82](https://www.ruby-forum.com/u/bryan82)\
**Post date:** [February 27, 2008, 1:06am UTC](https://www.ruby-forum.com/t/nested-resources-and-scoped-activerecord-calls/130280/1 "2008-02-27T01:06:08Z")

</div>

Ok, I’ve got a nested resource, set up like so:

map.resources :users, :has\_one =\> :profile

When I create a user, I’m using this route to go the new profile page:

new\_user\_profile\_path(@user)

I’m my Profiles controller, I’m using a before\_filter to determine the  
user:

before\_filter :load\_user  
def load\_user  
@user = User.find(params[:user\_id])  
end

And in my “new” action:

def new  
@profile = @user.profile.new  
end

In my view, I have:  
form\_for([@user, @profile]) do |f|

But I get an error telling me that @user.profile is nil.  
The :user\_id paramter is coming through fine, and @user has all the  
proper data in it. So is the problem with the scoped ActiveRecord call  
(@user.profile.new)?

If I use @profile = Profile.new, I get an error telling me  
“user\_profiles\_path” in invalid (which I guess is being generated by the  
form\_for). This is correct, since User should only have one Profile, not  
the plural “profiles.”

Anyone know what’s going on?

---

<div class="post-metadata">

**Author:** ![bryan82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a87d85/32.png) [@bryan82](https://www.ruby-forum.com/u/bryan82)\
**Post date:** [February 27, 2008, 4:15pm UTC](https://www.ruby-forum.com/t/nested-resources-and-scoped-activerecord-calls/130280/2 "2008-02-27T16:15:12Z")

</div>

Have you tried simply using:

form\_for @profile do |f|

If there is only one route to the profile (the nested one) I believe  
it will correctly build the path for you.

On Feb 26, 7:06 pm, “Bryan M.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![bryan82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a87d85/32.png) [@bryan82](https://www.ruby-forum.com/u/bryan82)\
**Post date:** [February 28, 2008, 5:49pm UTC](https://www.ruby-forum.com/t/nested-resources-and-scoped-activerecord-calls/130280/3 "2008-02-28T17:49:29Z")

</div>

Then it sounds like you want the more explicit version:

form\_for :profile, :url=\>user\_profile\_path(@user, @profile), … do |  
f|  
…  
end

On Feb 27, 6:02 pm, “Bryan M.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![bryan82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a87d85/32.png) [@bryan82](https://www.ruby-forum.com/u/bryan82)\
**Post date:** [February 28, 2008, 12:02am UTC](https://www.ruby-forum.com/t/nested-resources-and-scoped-activerecord-calls/130280/4 "2008-02-28T00:02:25Z")

</div>

AndyV wrote:

> Have you tried simply using:
> 
> form\_for @profile do |f|

No such luck. It tries to route to “profile\_path”, which doesn’t exist.

---

<div class="post-metadata">

**Author:** ![bryan82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a87d85/32.png) [@bryan82](https://www.ruby-forum.com/u/bryan82)\
**Post date:** [February 29, 2008, 4:42am UTC](https://www.ruby-forum.com/t/nested-resources-and-scoped-activerecord-calls/130280/5 "2008-02-29T04:42:37Z")

</div>

AndyV wrote:

> Then it sounds like you want the more explicit version:
> 
> form\_for :profile, :url=\>user\_profile\_path(@user, @profile), … do |  
> f|  
> …

Still no luck. Looking at my log, I can see that @user.profile.new (or  
@user.profile = Profile.new) is trying to grab a pre-existing profile  
relative to the user from the database.

I cannot figure out how to instantiate a new profile on a user, and keep  
my nested routes intact.

---

<div class="post-metadata">

**Author:** ![bryan82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a87d85/32.png) [@bryan82](https://www.ruby-forum.com/u/bryan82)\
**Post date:** [February 29, 2008, 5:18pm UTC](https://www.ruby-forum.com/t/nested-resources-and-scoped-activerecord-calls/130280/6 "2008-02-29T17:18:11Z")

</div>

Sorry, I think I’ve been after the wrong end of the equation.

With a has\_one relationship you should be using  
@user.build\_profile(…)

On Feb 28, 10:42 pm, “Bryan M.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
