Skip to content

:has_many and :belongs_to

November 28, 2010

I find these highly unintuitive.

I have a User and an Address model.

I would say a User has one Address.

I have :address_id in User.

So I add the has_one :address in User and belongs_to Address

However, I understood that incorrectly. According to

http://hittingthebuffers.wordpress.com/2006/04/25/has_one-or-belongs_to/

“The table with the foreign key in it belongs_to the table that that foreign key references.”

Hence User belongs_to Address which sounds highly unintuitive, and Address has_one User

However consider a Blog Entry and Author

Each Blog Entry should have a :author_id

Hence BlogEntry belongs to Author and Author has_many Blog entries, which makes sense here

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: