state_machines-activerecord
state_machines-activerecord copied to clipboard
scopes not referencing aliased table names
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'))"