rwb*_*rwb 1 sql-server activerecord ruby-on-rails ruby-on-rails-3
说我有三个型号:
class Foo < ActiveRecord::Base
has_many :things
end
class Thing < ActiveRecord::Base
belongs_to :foo
belongs_to :other_thing
end
class OtherThing
has_many :things
end
Run Code Online (Sandbox Code Playgroud)
我怎样才能从以下方面出发Foo并急切地加载OtherThing:
Foo.includes([:things => [:other_things]})
我搜索过但找不到任何东西.
谢谢
包含和连接使用与您定义的关系相同的语法:
Foo.includes(:things => :other_thing)
Run Code Online (Sandbox Code Playgroud)
会工作因为:
Foo has_many :things
^
Thing belongs_to :other_thing
^^
Run Code Online (Sandbox Code Playgroud)
但请记住,在where子句中,始终使用复数版本:
Foo.includes(:things => :other_thing).where(other_things: { name: 'Bobby' })
^^ ^^
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1860 次 |
| 最近记录: |