我如何在X先生和Y先生之间建立关系以查看Y先生的职位? (查看建议的帖子)
user.rb
has_many :posts
has_many :liked_posts, through: :liked, source: :post
post.rb
def liked_by?(user)
likes.where(user: user).any?
end
likes.rb
# id :integer not null, primary key
# user_id :integer not null
# like_id :integer not null
我应该使用uniq吗?
答案 0 :(得分:0)
如果您的意思是我,我不是100%确定,但如果您想将User
与Post
关联到Like
,请继续执行:
user.rb
has_many :posts
has_many :likes
has_many :liked_posts, through: :likes
post.rb
has_many :likes
def liked_by?(user)
self.likes.where(user: user).any?
end
likes.rb
belongs_to :user
belongs_to :post
您必须添加相应的数据库迁移才能将属性添加到likes