如何访问“:has_many :though”使用 to_json 时连接表数据?
我有三个模型(此处简化):
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
我使用 Rails 的 to_json 方法将其发送到一些 JavaScript,如下所示:
render :layout => false , :json => @child.to_json(
:include => {
:observations => {
:include => :photos,
:methods => [:key, :title, :subtitle]
}
},
:except => [:password]
)
这完美地工作。通过连接表(子视图)可以很好地检索观察结果。
但是,我还想获取位于子视图连接表中的数据;特别是“needs_edit”的值。
我不知道如何在 to_json 调用中获取这些数据。
谁能帮助我吗?非常感谢。
奎斯
I have three models (simplified here):
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
I'm sending this to some JavaScript using Rails' to_json method like this:
render :layout => false , :json => @child.to_json(
:include => {
:observations => {
:include => :photos,
:methods => [:key, :title, :subtitle]
}
},
:except => [:password]
)
This works perfectly. Observations are retrieved fine 'through' the join table (childviews).
However, I also want to get at data that sits in the childviews join table; specifically the value for 'needs_edit'.
I can't figure out how to get at this data in a to_json call.
Can anyone help me? Many thanks in advance.
qryss
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定,但这不应该起作用吗?
编辑:
这也可能有效,因为子视图属于过度:
Not sure, but shouldn't this work?
EDIT:
This might work too, since childviews belongs_to the overvation:
感谢 Rock 的指点 - 我现在已经可以使用了!
这段代码:
给了我这个输出(为了清晰起见缩写):
谢谢你,Rock!这让我很头疼。
:)
qryss
Thanks to Rock for the pointers - I now have it working!
This code:
gives me this output (abbreviated for clarity):
Thank you, Rock! That was doing my head in.
:)
qryss