【发布时间】:2015-09-23 15:51:24
【问题描述】:
我想用属于范围一部分的书籍返回用户 -
class User < ActiveRecord::Base
has_many :books
end
class Book < ActiveRecord::Base
belongs_to :user
scope :published, -> { where (status: 'Published') }
end
所以
Book.published
返回所有已出版的书籍。我正在尝试定义拥有一本或多本已出版书籍的所有用户的范围。
知道
User.joins(:books).uniq.all
返回所有用户一本书(来自Rails: How to get objects with at least one child?) - 我可以添加一个范围,还是有更好的方法?
【问题讨论】:
-
所以你希望所有有孩子的父母都在
attends_club范围内?
标签: ruby-on-rails postgresql activerecord