gpt4 book ai didi

ruby-on-rails - 如何在事件记录 to_json 中包含嵌套和同级关联?

转载 作者:行者123 更新时间:2023-12-01 16:10:26 27 4
gpt4 key购买 nike

我有一个类(class)模型,与另一个模型树有 2 个关联:

belongs_to  :interaction_outline, :class_name => "Tree", 
:foreign_key => "interaction_outline_id"
belongs_to :token_outline, :class_name => "Tree",
:foreign_key => "token_outline_id"

我读到this并且能够在我的 Controller 中包含兄弟关联。

@course.to_json(:include=> [:interaction_outline, :token_outline]

我还能够获得多重嵌套关联:

@course.to_json(:include=>{:interaction_outline=> 
{:include=> {:tree_node=>
{:include=> :definition}}}} )

但是我无法同时获得同级和多重嵌套包括:

@course.to_json (:include=> [{:interaction_outline=> 
{:include=> {:tree_node=>
{:include=> :definition}}}},
{:token_outline=>
{:include=> {:tree_node=>
{:include=> :definition}}}} ] )
#NoMethodError (undefined method `macro' for nil:NilClass)
#the error you get when the syntax or the association is wrong

我也尝试过这个:

@course.to_json (:include=> [:interaction_outline=> 
{:include=> {:tree_node=>
{:include=> :definition}}},
:token_outline=>
{:include=> {:tree_node=>
{:include=> :definition}}} ] )
#same error

这里正确的语法是什么?

最佳答案

你们真的很接近。只需使用哈希符号而不是数组即可。

@course.to_json (:include=> {:interaction_outline=> 
{:include=> {:tree_node=>
{:include=> :definition}}},
:token_outline=>
{:include=> {:tree_node=>
{:include=> :definition}}}} )

关于ruby-on-rails - 如何在事件记录 to_json 中包含嵌套和同级关联?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6755080/

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