Rails: belongs_to vs has_one

19,221

Solution 1

Yes, I think you've just found a slightly odd-looking scenario in Rails. I suppose it might be useful to view "status" as a sort of category to which the bug belongs — in that light, it makes sense.

Solution 2

You didn't explain precisely what kind of relationship between Bug and Status you would like to get, but I assume you are interested in one of the following:

  • one-to-many: in this case there should be has_many in Bug class and belongs_to in Status class,
  • one-to-one: in this case there should be has_one in Bug class and belongs_to in Status class.

In both cases Status contains the foreign key. In the second case the wording is a little odd, due to the fact that one-to-one relationship is in fact asymmetric (there should be a FK on one side only).

Share:
19,221
Rinzler
Author by

Rinzler

I have experience in JSP/Java/Oracle, and ASP.net/C#/Sql Server. I am a rubyist, a clojurian, a rustacean, and work with rails professionally. I would consider myself a javascript programmer, and my expertise is front end web development. I am also a big fan of both emacs and parts of vim. You can read my blog at http://mattbriggs.net

Updated on June 19, 2022

Comments

  • Rinzler
    Rinzler almost 2 years

    A bit of a newbie question on rails associations.

    I have a Bug model, and a Status model. Status is basically just a key/value pair table. Out of the choices available, I would say Bug has_one Status makes the most sense. However, according to this

    Content belongs_to ContentTemplate. Go back and look at how I described the problem, and you'll see that it works. With belongs_to, the table accepts responsibility for the foreign key. So Content has a content_template_id. And ContentTemplate doesn't need anything. I can point to it at will. Done.

    Bug belongs_to Status would be more appropriate (since Bug should take the foreign key). Semantically, his example makes sense, but mine makes none. Is this just a quirk of rails where in this situation it looks odd, or am I not understanding something/doing it wrong?

  • Rinzler
    Rinzler almost 15 years
    That is for many-to-many relationships, which this is not. This is a many-to-one. My question is basically around that the wording for that relationship only makes sense in a one-to-many, not a many-to-one, and if there is a more elegant way to handle it.
  • rnicholson
    rnicholson almost 15 years
    Understood. I guess my typical thinking of "Bugs" and their "Status" is that a Bug could be in multiple statuses at once (ex. "worksforme" and "open") or you may want to keep the history of a Bug's status.
  • Chuck
    Chuck almost 15 years
    The problem is that a bug does not have many statuses at a time, either conceptually or in a proper implementation. Conceptually, we think of a status as belonging to many bugs, but Rails can only express this as a status having many bugs.
  • Rinzler
    Rinzler almost 15 years
    I guess it stands as a testament to how nicely rails works semantically, that hitting this situation I was like "I must be doing it wrong"
  • Rinzler
    Rinzler almost 15 years
    @chuck: That is pretty much it. One-to-Many vs Many-to-One. Logically, pretty much equivalent, but conceptually there is a difference