Ruby on rails - Reference the same model twice?

前端 未结 4 1653
长情又很酷
长情又很酷 2020-11-30 18:10

Is it possible to set up a double relationship in activerecord models via the generate scaffold command?

For example, if I had a User

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-30 18:53

    Add this to your Model

    belongs_to :sender, :class_name => "User"
    belongs_to :recipient, :class_name => "User"
    

    And you are able to call @message.sender and @message.recipient and both reference to the User model.

    Instead of user:references in your generate command, you'd need sender:references and recipient:references

提交回复
热议问题