1
如何在与连接关联的where子句中使用命名关联?ActiveRecord ::关系不能在连接的where子句中使用命名关联
class Pet < ActiveRecord::Base
belongs_to :owner
end
class Owner < ActiveRecord::Base
has_many :dogs, :class_name => 'Pet', :foreign_key => :owner_id
end
Owner.joins(:dogs).where(:dogs => {:name => 'fido'}).to_sql
产生:
"SELECT `owners`.* FROM `owners` INNER JOIN `pets` ON `pets`.`owner_id` = `owners`.`id` WHERE (`dogs`.`name` = 'fido')"
注意,WHERE
子句看在dogs
表代替pets
表
的参考:
http://guides.rubyonrails.org/active_record_querying.html#specifying-conditions-on-the-joined-tables