gpt4 book ai didi

ruby-on-rails - 使用to_json时如何访问 ':has_many :though'连接表数据?

转载 作者:行者123 更新时间:2023-12-04 05:58:06 24 4
gpt4 key购买 nike

我有三个模型(这里简化):

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]
)

这非常有效。观察结果可以“通过”连接表( subview )很好地检索。

然而,我还想获取位于 childviews 连接表中的数据;特别是“needs_edit”的值。

我不知道如何在 to_json 调用中获取这些数据。

谁能帮帮我?非常感谢。

qryss

最佳答案

不确定,但这不应该有效吗?

@child.to_json(
:include => {
:observations => {
:include => :photos,
:methods => [:key, :title, :subtitle]
},
:childviews => { :only => :needs_edit }
},
:except => [:password]
)

编辑:这也可能有效,因为 childviews belongs_to the overvation:

@child.to_json(
:include => {
:observations => {
:include => { :photos, :childviews => { :only => :needs_edit } }
:methods => [:key, :title, :subtitle]
}
},
:except => [:password]
)

关于ruby-on-rails - 使用to_json时如何访问 ':has_many :though'连接表数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3433631/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com