state_machines-activerecord icon indicating copy to clipboard operation
state_machines-activerecord copied to clipboard

scopes not referencing aliased table names

Open mengqing opened this issue 7 years ago • 0 comments

say if I have the following associations

class User < ApplicationRecord
  has_many :active_projects, -> { with_state(:active) }, class_name: Project
end

class Project < ApplicationRecord
  belongs_to :user

  state_machine :state, initial: :pending do
    state :active
  end
end

And if I do the following query

> Project.joins(user: :active_projects).to_sql
=> "SELECT `projects`.* FROM `projects` INNER JOIN `users` ON `users`.`id` = `projects`.`user_id` INNER JOIN `projects` `active_projects_users` ON `active_projects_users`.`user_id` = `users`.`id` AND (`projects`.`state` IN ('active'))"

mengqing avatar Jun 14 '17 07:06 mengqing