在我的 Post 模型中,我有
has_many :followers
在我的追随者模型中,我有
belongs_to :model
belongs_to :owner,polymorphic: true
在我的用户和管理员设计模型中,我有
has_many :followers,as: :owner
要求:我想要类似的东西Post.owners
,它应该返回一个所有关注这篇文章的用户和/或管理员的列表。
我不确定,但我认为 AR 并没有提供一种仅在一个查询中加载多态关联的方法。但是你可以使用:
post = Post.find(1)
post_followers = post.followers.includes(:owner)
post_owners = post_followers.map(&:owner)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句