Search code examples
ruby-on-railsassociationsbelongs-tohas-one

Rails: unidirectional association


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?


Solution

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