【问题标题】:How to access ':has_many :though' join table data when using to_json?使用 to_json 时如何访问 ':has_many :through' 连接表数据?
【发布时间】:2010-08-08 08:37:30
【问题描述】:

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

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

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

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

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

谁能帮帮我?非常感谢。

qryss

【问题讨论】:

    标签: ruby-on-rails json has-many-through model-associations


    【解决方案1】:

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

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

    【讨论】:

    • 嘿摇滚,感谢您的帮助。这几乎是正确的(并且足以让我继续前进)。我从中得到的是一组“needs_edit”标志,我可以将其映射到观察数组上。我希望得到的是实际在 JSON 哈希中用于相关观察的“needs_edit”标志。不重要;我可以忍受你给我的一切,所以谢谢你让我走到这一步! qryss
    • 哦,你有一个小错误:'=>',而不是'='。
    • 也许您可以将 :childviews => {:only=>:needs_edit} 放入 :obervations 哈希中? (见编辑)对于给定的观察,这应该包括 needs_edit 值,但我不确定。
    【解决方案2】:

    感谢 Rock 的指点 - 我现在可以正常工作了!

    这段代码:

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

    给我这个输出(为了清楚起见,缩写):

    {
        "child":
        {
            "foo":"bar",
            "observations":
            [
                {
                    "foo2":"bar2",
                    "photos":
                    [
                        {
                            "foo3":"bar3",
                        }
                    ],
                    "childviews":
                    [
                        {
                            "needs_edit":true
                        }
                    ]
                }
            ]
        }
    }
    

    谢谢你,洛克!这让我很头疼。

    :)

    qryss

    【讨论】:

      猜你喜欢
      • 2013-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-11
      • 1970-01-01
      • 2019-10-29
      相关资源
      最近更新 更多