You can hand an ActiveRecord::Relation
to most scope methods to build complex queries. E.g.:
Post.where(status: 'published', user: User.where(admin: true)).includes(:user)
This should generate two queries; one will include a subquery for users with the admin condition, and the other will be to load the users (eager loading).
Note that you can simplify these queries quite a bit with scopes, which keep the nitty gritty details of your columns the responsibility of the models:
class User < ApplicationRecord
scope :admin, -> { where(admin: true) }
end
class Post < ApplicationRecord
scope :published, -> { where(status: 'published') }
end
# posts from admin users, with eager loaded users
Post.published.where(user: User.admin).includes(:user)
Cheers!