使用select with join查询

时间:2017-06-27 17:24:29

标签: ruby-on-rails activerecord

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语句中尝试了相同的结果。

0 个答案:

没有答案