Multiple has_many relationships to same model

后端 未结 4 2021
抹茶落季
抹茶落季 2021-02-04 09:07

I have a model User that can create Posts

User
 has_many :posts
Post
 belongs_to :user

However, I want to also allow users to save posts as boo

4条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-04 09:34

    How do you get around this problem?

    By giving your associations unique names. It's not that you can't unambiguously access them, it's that your second one is destroying the first one.

    Instead of calling both posts, use bookmarked_posts for your second association:

    has_many :bookmarked_posts, through: :bookmarks, source: :posts
    

提交回复
热议问题