Rails 5
class Car
has_many :car_user_roles, dependent: :destroy
has_many :users, -> { distinct }, through: :car_user_roles
scope :with_subscription, -> { includes(:subscription).where.not(subscriptions: {id: nil}) }
end
class CarUserRole
belongs_to :car
belongs_to :user
end
class User
has_many :car_user_roles, dependent: :destroy
has_many :cars, -> { distinct }, through: :car_user_roles
end
Car.with_subscription.joins(car_user_roles: :user).where(car_user_roles: {role: 'driver'}).select("cars.id").first == Car.with_subscription.joins(car_user_roles: :user).where(car_user_roles: {role: 'driver'}).first
是真的
为什么select子句不起作用?我在include语句中尝试了相同的结果。