qry*_*yss 3 json ruby-on-rails has-many-through model-associations
我有三个模型(这里简化):
class Child < ActiveRecord::Base
has_many :childviews, :dependent => :nullify
has_many :observations, :through => :childviews
end
class Childview < ActiveRecord::Base
belongs_to :observation
belongs_to :child
end
class Observation < ActiveRecord::Base
has_many :childviews, :dependent => :nullify
has_many :children, :through => :childviews
end
Run Code Online (Sandbox Code Playgroud)
我正在使用Rails的to_json方法将此发送给一些JavaScript,如下所示:
render :layout => false , :json => @child.to_json(
:include => {
:observations => {
:include => :photos,
:methods => [:key, :title, :subtitle]
}
},
:except => [:password]
)
Run Code Online (Sandbox Code Playgroud)
这非常有效.通过连接表(子视图)可以很好地检索观察结果.
但是,我还想获取位于childviews连接表中的数据; 特别是'needs_edit'的值.
我无法弄清楚如何在to_json调用中获取此数据.
谁能帮我?提前谢谢了.
qryss
不确定,但不应该这样吗?
@child.to_json(
:include => {
:observations => {
:include => :photos,
:methods => [:key, :title, :subtitle]
},
:childviews => { :only => :needs_edit }
},
:except => [:password]
)
Run Code Online (Sandbox Code Playgroud)
编辑:这可能也有效,因为childviews属于overvation:
@child.to_json(
:include => {
:observations => {
:include => { :photos, :childviews => { :only => :needs_edit } }
:methods => [:key, :title, :subtitle]
}
},
:except => [:password]
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1513 次 |
| 最近记录: |