Вопрос

Here is a scenario: I have an IssueType model and an IssueTypeColour model. An IssueType has_one IssueTypeColour, but an IssueTypeColour doesn't necessarily belongs_to a particular IssueType; it can belong to many different IssueType. In other words, many IssueTypes can have the same IssueTypeColour.

According to this scenario, it makes sense to have a has_one association on the IssueType model and no belongs_to association on the IssueTypeColour model.

But is that acceptable/OK?

Это было полезно?

Решение

I am wondering if you really need that IssueTypeColor model, you can just add TypeColour attribute to IssueType.

In the case you want to use the two models, your relation is really one to many, so you would need:

IssueType :belongs_to ....
IssueTypeColour :has_many ...

I would recommend you this link:

http://guides.rubyonrails.org/association_basics.html

There you will find a detailed explanation.

Другие советы

For me it's okay. In my scenario, i've the ticket model that has one airline, but the airline doesn't belongs to a ticket because can be more than one ticket that has that airline. I fix this putting a foreign key ticket_id in airline model. So, when i have the ticket object i can request the airline associated to this ticket. But, when i have the airline object i can't request the ticket because the associetion is unidirectional from the side of the ticket object. Besides, i believe that is not a good idea establish bidirectional association when is not necesary. But well, not all scenaries are the same.

This is the migration file:

add_reference :airlines, :ticket, index: true
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top