5
我想提出一些疑问,就像这样:查询通过协会 - 的Rails 3
employees = Employee.where(:group.name => 'admin')
employees = Employee.where(:company.address.city => 'Porto Alegre')
我的意思是,我需要访问是通过关联的另一示范田。
在此先感谢
我想提出一些疑问,就像这样:查询通过协会 - 的Rails 3
employees = Employee.where(:group.name => 'admin')
employees = Employee.where(:company.address.city => 'Porto Alegre')
我的意思是,我需要访问是通过关联的另一示范田。
在此先感谢
假设一个公司可以有多个地址(这我假设,因为你company.address.city命名空间的,因为它使一个有趣的查询示例):
class Group < ActiveRecord::Base
has_many :employees
end
class Employee < ActiveRecord::Base
belongs_to :group
belongs_to :company
end
class Company < ActiveRecord::Base
has_many :employees
has_many :addresses
end
class Address < ActiveRecord::Base
belongs_to :company
end
你要找的查询将如下所示:
Employee.
joins(:group).
where(:groups => { :name => 'admin' })
Employee.
joins(:company => :addresses).
where(:addresses => { :city => 'Porto Alegre' })
注意,在该协会的复数形式上面的where子句中总是使用。 where子句中的键指的是表名,而不是关联名。