# Help decoupling models with a foreign key relationship

**URL:** <https://www.ruby-forum.com/t/help-decoupling-models-with-a-foreign-key-relationship/183232>\
**Category:** Rails\
**Created:** [February 10, 2010, 9:14am UTC](https://www.ruby-forum.com/t/help-decoupling-models-with-a-foreign-key-relationship/183232 "2010-02-10T09:14:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![robo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bb73d2/32.png) [@robo](https://www.ruby-forum.com/u/robo)\
**Post date:** [February 10, 2010, 9:14am UTC](https://www.ruby-forum.com/t/help-decoupling-models-with-a-foreign-key-relationship/183232/1 "2010-02-10T09:14:26Z")

</div>

Recently started working with rails; I’m very impressed so far, but I  
do have a question -

Say I’m running a blog-type application. I’ve got a Users table, and  
a Posts table. Each post on the blog is made by a user. The Post  
table has an FK user\_id that keeps track of the author of the post.  
This is all well and good, but I’ve found myself doing this a lot:

class Post  
def get\_author\_name  
user = User.find(user\_id)  
user.name  
end  
end

which definitely works for retrieving the author name when I’m  
rendering my view, but I’m worried I’m going to be weaving my classes  
too tightly, especially given how often this type of scenario can come  
up.

Does anyone have a better approach?

Thanks!

---

<div class="post-metadata">

**Author:** ![robo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bb73d2/32.png) [@robo](https://www.ruby-forum.com/u/robo)\
**Post date:** [February 10, 2010, 11:44am UTC](https://www.ruby-forum.com/t/help-decoupling-models-with-a-foreign-key-relationship/183232/2 "2010-02-10T11:44:01Z")

</div>

class Post  
belongs\_to :user  
end

Then you can do post.user.name retrieve the name of the author of the  
post

---

<div class="post-metadata">

**Author:** ![robo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bb73d2/32.png) [@robo](https://www.ruby-forum.com/u/robo)\
**Post date:** [February 10, 2010, 2:48pm UTC](https://www.ruby-forum.com/t/help-decoupling-models-with-a-foreign-key-relationship/183232/3 "2010-02-10T14:48:03Z")

</div>

That’s handy! Does that work too if I say

class Post  
has\_one :user  
end

I guess the real question is - what’s the difference between has\_one  
and belongs\_to?

---

<div class="post-metadata">

**Author:** ![robo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bb73d2/32.png) [@robo](https://www.ruby-forum.com/u/robo)\
**Post date:** [February 10, 2010, 3:07pm UTC](https://www.ruby-forum.com/t/help-decoupling-models-with-a-foreign-key-relationship/183232/4 "2010-02-10T15:07:04Z")

</div>

robo wrote:

> That’s handy! Does that work too if I say
> 
> class Post  
> has\_one :user  
> end
> 
> I guess the real question is - what’s the difference between has\_one  
> and belongs\_to?

The location of the foreign key. Go read the Associations docs.

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
