Rails: belongs_to vs has_one?

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.

3 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" – Matt Briggs May 23 '09 at 15:17.

TABLE: Bug id integer desc string status_id integer fk Status id integer desc string RAILS MODEL: Bug belongs_to :status Status has_many :bugs.

2 Bug would take the foreign key because a status can have many bugs, but a bug has only one status at a time. – Chuck May 22 '09 at 21:44 Your new solution is how we think of the situation, but it will not work. When you do bug.

Status, it will look for a bug_id column in status, which does not exist. A has_one or has_many needs to be matched by a belongs_to in the class that is being "had". – Chuck May 22 '09 at 23:55 How would you change it?

Feel free to copy my answer into yours and make changes there if you prefer. I'm just curious to know what you think the models should look like. – Willem Obst May 23 '09 at 1:18 It should be Bug.

Belongs_to :status, like the asker guessed. It looks awkward, but that's the way Rails needs the relationship to be expressed AFAIK. The belongs_to means "my table has the foreign key."

That way bug. Status and status. Bugs will both work correctly.

– Chuck May 23 '09 at 6:30 Thanks for the clarification. – Willem Obst May 23 '09 at 21:32.

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).

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. – Chuck May 22 '09 at 23:30 @chuck: That is pretty much it.

One-to-Many vs Many-to-One. Logically, pretty much equivalent, but conceptually there is a difference – Matt Briggs May 23 '09 at 15:20.

If Status is just a look-up/key-value table, it sounds like you might want a habtm(has_and_belongs_to_many) relationship between Status and Bug. With habtm, what you'll end up with is a bugs_statuses join table that has bug_id and status_id columns along with your bugs and statuses tables.

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.

– Matt Briggs May 22 '09 at 21:29 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.

– rnicholson May 22 '09 at 22:05.

Content belongs_to ContentTemplate. Problem, and you'll see that it works. Responsibility for the foreign key.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions