Rails:如何实现一个命名范围,返回至少写入一条消息的所有用户?
考虑以下关联:
class User < ActiveRecord::Base
has_many :messages, :foreign_key => "publisher_id"
scope :wrote_at_least_one_message, <??>
end
class Message < ActiveRecord::Base
belongs_to :publisher, :class_name => "User"
end
如何实现 wrote_at_least_one_message
命名范围?
Consider the following associations:
class User < ActiveRecord::Base
has_many :messages, :foreign_key => "publisher_id"
scope :wrote_at_least_one_message, <??>
end
class Message < ActiveRecord::Base
belongs_to :publisher, :class_name => "User"
end
How could I implement the wrote_at_least_one_message
named scope ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个怎么样?内连接将检查消息表中至少一项。区别是为了避免重复。
How about this? The inner join would check for at least one entry in messages table. Distinct is to avoid duplicates.