top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is difference between belongs_to and has_one in Rails?

0 votes
320 views

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

posted Jun 30, 2014 by Kiran Kumar

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

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.

answer Jun 30, 2014 by Sheetal Chauhan
Similar Questions
+2 votes

I am not finding any difference between the 2 methods - in_groups and in_groups_of. Is their really any difference between in_groups and in_groups_of

+2 votes

I am working on building an api client for the Rakuten Market-place. I have got some test requests for add delete update etc working and thought I should aim to structure it as a Gem and publish it so that others can use it/enhance it.

I haven't built a Gem before (worked mostly within the rails environment to date).

Have been reading and looking at other api client gems and am making progress on building something (still got a way to go to handle errors etc.)

To make the gem general purpose though, I am trying to figure out the best way to provide the mapping between models in a rails app and the api client objects (such as product, category, order etc).

I am aiming to make each client api object a class be (or should i call them models?)
Then I suspect I will use new to build an api instance from a rails instance, and find to return an api instance to a rails instance.

I can build in mapping for my own models to the api objects with no problem, but I can't see how I could generalize this so that other apps with similar models could use the gem.

I suspect there are approaches for doing this, but so far I haven't managed to come up with how to do it. If anyone has any wisdom on the matter I would be grateful.

...