What is difference between belongs_to and has_one in Rails?

84 views
Skip to first unread message

Jaimin Pandya

unread,
Jun 30, 2014, 9:47:14 AM6/30/14
to rubyonra...@googlegroups.com
What is a difference between belongs_to and has_one?

How can I decide belongs_to use to X(assume) model and has_one use to
Y(assume) model?

Kind regards.

--
Posted via http://www.ruby-forum.com/.

Colin Law

unread,
Jun 30, 2014, 10:24:42 AM6/30/14
to rubyonra...@googlegroups.com
On 30 June 2014 14:45, Jaimin Pandya <li...@ruby-forum.com> wrote:
> What is a difference between belongs_to and has_one?

They are at opposite ends of the association. The model that
belongs_to is the one that has the Y_id field in the database.

>
> How can I decide belongs_to use to X(assume) model and has_one use to
> Y(assume) model?

Do you mean how to decide whether X belongs_to Y and Y has_one X
versus Y belongs_to X and X has_one Y? If so then it is just a matter
of which seems to fit best for you. Of course a third alternative is
to simplify the code and the database and combine the models into XY.

Colin

Jaimin Pandya

unread,
Jul 8, 2014, 11:52:00 AM7/8/14
to rubyonra...@googlegroups.com
Colin Law wrote in post #1151152:
Thank you for your reply.

I am looking for difference which is explained by following link:

http://stackoverflow.com/questions/861144/difference-between-has-one-and-belongs-to-in-rails
Reply all
Reply to author
Forward
0 new messages