# Has One - Belongs to question

**URL:** <https://www.ruby-forum.com/t/has-one-belongs-to-question/77866>\
**Category:** Rails\
**Created:** [December 1, 2006, 2:54am UTC](https://www.ruby-forum.com/t/has-one-belongs-to-question/77866 "2006-12-01T02:54:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bparanj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e9a140/32.png) [@bparanj](https://www.ruby-forum.com/u/bparanj)\
**Post date:** [December 1, 2006, 2:54am UTC](https://www.ruby-forum.com/t/has-one-belongs-to-question/77866/1 "2006-12-01T02:54:24Z")

</div>

I have a User model that has\_one BillingProfile and BillingProfile  
belongs to a User. How do I retrieve the billing profile corresponding  
to that particular user? TIA.

---

<div class="post-metadata">

**Author:** ![bparanj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e9a140/32.png) [@bparanj](https://www.ruby-forum.com/u/bparanj)\
**Post date:** [December 1, 2006, 2:56am UTC](https://www.ruby-forum.com/t/has-one-belongs-to-question/77866/2 "2006-12-01T02:56:24Z")

</div>

class User \< ActiveRecord::Base  
has\_one :billing\_profile  
end

class BillingProfile \< ActiveRecord::Base  
belongs\_to :user  
end

user = User.find(:first)  
user.billing\_profile

---

<div class="post-metadata">

**Author:** ![bparanj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e9a140/32.png) [@bparanj](https://www.ruby-forum.com/u/bparanj)\
**Post date:** [December 1, 2006, 3:34am UTC](https://www.ruby-forum.com/t/has-one-belongs-to-question/77866/3 "2006-12-01T03:34:30Z")

</div>

I generated the User model by using RESTful Authentication plugin. I  
ran the ./script/generate authenticated user sessions command and it  
generated the migration for users table. I ran the rake db:migrate. I  
also defined the has\_one and belongs\_to in the User and BillingProfile  
class.

My question is currently the BillingProfile table does not have the  
user\_id foreign key. Do I need to create a separate migration for  
that? Thanks again.

---

<div class="post-metadata">

**Author:** ![bparanj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e9a140/32.png) [@bparanj](https://www.ruby-forum.com/u/bparanj)\
**Post date:** [December 1, 2006, 5:18pm UTC](https://www.ruby-forum.com/t/has-one-belongs-to-question/77866/4 "2006-12-01T17:18:36Z")

</div>

In my User object I am saving the login id of the user, (after the user  
logs  
in successfully)

session[:user\_key] = user.id

In my BillingProfile show method, I want to retrieve the billling  
profile  
for that particular user:

billing\_profile = BillingProfile.find(:first, :conditions =\>  
"user\_id=‘user\_key’ ")

Is the above code the right way to do it? TIA.

---

<div class="post-metadata">

**Author:** ![bparanj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e9a140/32.png) [@bparanj](https://www.ruby-forum.com/u/bparanj)\
**Post date:** [December 1, 2006, 6:36am UTC](https://www.ruby-forum.com/t/has-one-belongs-to-question/77866/5 "2006-12-01T06:36:49Z")

</div>

Yes, the billing\_profiles table must have a column of type integer,  
named  
user\_id. It will act as a foreign key to the users table.

---

<div class="post-metadata">

**Author:** ![bparanj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e9a140/32.png) [@bparanj](https://www.ruby-forum.com/u/bparanj)\
**Post date:** [December 1, 2006, 10:43pm UTC](https://www.ruby-forum.com/t/has-one-belongs-to-question/77866/6 "2006-12-01T22:43:50Z")

</div>

The way I would probably do it is something like:

@user = User.find(session[:user\_key])  
@billing\_profile = @user.billing\_profile

The first line sets the @user instance variable the User object with id  
session[:user\_key].  
The second line grabs a BillingProfile object, in particular, the one  
with  
user\_id = @user.id . It would probably be a good exercise to look at  
the  
SQL queries generated for these calls.

---

<div class="post-metadata">

**Author:** ![bparanj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e9a140/32.png) [@bparanj](https://www.ruby-forum.com/u/bparanj)\
**Post date:** [December 1, 2006, 10:47pm UTC](https://www.ruby-forum.com/t/has-one-belongs-to-question/77866/7 "2006-12-01T22:47:52Z")

</div>

That’s the way to do it…  
but you might consider using eager loading so you avoid an extra DB call  
(if  
you can)

@user = User.find(session[:user\_key], :include=\>[:billing\_profile])  
@billing\_profile = @user.billing\_profile
