如何在rails控制台显示所有关联集合?
How to display all association collection in rails console?
我想在rails控制台中显示parent实体“文章”的所有集合,这将是它们之间的关系,示例:
# article.rb
class Article < ActiveRecord::Base
belongs_to :parent, :class_name => 'Article', optional: true
has_many :sub_articles, :class_name => 'Article', :foreign_key => 'parent_id'
end
现在我拥有的是:
irb(main):095:0> Article.find(1)
Article Load (0.9ms) SELECT "articles".* FROM "articles" ORDER BY
"articles"."id" ASC LIMIT [["LIMIT", 1]]
=>
#<Articles:0x0000264231faa292
id: 1,
name: "king article"
created_at: Fri, 13 Aug 2021 13:14:26.463429000 UTC +00:00,
updated_at: Fri, 13 Aug 2021 13:14:26.463429000 UTC +00:00,
parent_id: nil
我想在 rails 控制台上显示的内容:
id: 1,
name: "king article"
created_at: Fri, 13 Aug 2021 13:14:26.463429000 UTC +00:00,
updated_at: Fri, 13 Aug 2021 13:14:26.463429000 UTC +00:00,
parent_id: nil
sub_articles:
[ Article:0x0000641428defb71
id: 2,
name: "pencils article"
created_at: Fri, 13 Aug 2021 13:14:26.463429000 UTC +00:00,
updated_at: Fri, 13 Aug 2021 13:14:26.463429000 UTC +00:00,
parent_id: 1
sub_articles:
[ Article:0x0000621438defb71
id: 3,
name: "pencil child 1"
created_at: Fri, 13 Aug 2021 13:14:26.463429000 UTC +00:00,
updated_at: Fri, 13 Aug 2021 13:14:26.463429000 UTC +00:00,
parent_id: 2
sub_articles: [],
id: 4,
name: "pencil child 2"
created_at: Fri, 13 Aug 2021 13:14:26.463429000 UTC +00:00,
updated_at: Fri, 13 Aug 2021 13:14:26.463429000 UTC +00:00,
parent_id: 2
sub_articles: []
]
]
最终我要寻找的是是否咨询 parent 以显示他们的 children(如果可能的话 children 的 children)
可能最好通过在顶级文章上调用 to_json
并配置 Article
的 as_json
方法来包含 sub_articles
class Article < ActiveRecord::Base
def as_json(options={})
super(methods: [:subarticles])
end
end
在 rails 控制台中:
$> Article.find(3).to_json # => should give you the hierarchy you're looking for
我想在rails控制台中显示parent实体“文章”的所有集合,这将是它们之间的关系,示例:
# article.rb
class Article < ActiveRecord::Base
belongs_to :parent, :class_name => 'Article', optional: true
has_many :sub_articles, :class_name => 'Article', :foreign_key => 'parent_id'
end
现在我拥有的是:
irb(main):095:0> Article.find(1)
Article Load (0.9ms) SELECT "articles".* FROM "articles" ORDER BY
"articles"."id" ASC LIMIT [["LIMIT", 1]]
=>
#<Articles:0x0000264231faa292
id: 1,
name: "king article"
created_at: Fri, 13 Aug 2021 13:14:26.463429000 UTC +00:00,
updated_at: Fri, 13 Aug 2021 13:14:26.463429000 UTC +00:00,
parent_id: nil
我想在 rails 控制台上显示的内容:
id: 1,
name: "king article"
created_at: Fri, 13 Aug 2021 13:14:26.463429000 UTC +00:00,
updated_at: Fri, 13 Aug 2021 13:14:26.463429000 UTC +00:00,
parent_id: nil
sub_articles:
[ Article:0x0000641428defb71
id: 2,
name: "pencils article"
created_at: Fri, 13 Aug 2021 13:14:26.463429000 UTC +00:00,
updated_at: Fri, 13 Aug 2021 13:14:26.463429000 UTC +00:00,
parent_id: 1
sub_articles:
[ Article:0x0000621438defb71
id: 3,
name: "pencil child 1"
created_at: Fri, 13 Aug 2021 13:14:26.463429000 UTC +00:00,
updated_at: Fri, 13 Aug 2021 13:14:26.463429000 UTC +00:00,
parent_id: 2
sub_articles: [],
id: 4,
name: "pencil child 2"
created_at: Fri, 13 Aug 2021 13:14:26.463429000 UTC +00:00,
updated_at: Fri, 13 Aug 2021 13:14:26.463429000 UTC +00:00,
parent_id: 2
sub_articles: []
]
]
最终我要寻找的是是否咨询 parent 以显示他们的 children(如果可能的话 children 的 children)
可能最好通过在顶级文章上调用 to_json
并配置 Article
的 as_json
方法来包含 sub_articles
class Article < ActiveRecord::Base
def as_json(options={})
super(methods: [:subarticles])
end
end
在 rails 控制台中:
$> Article.find(3).to_json # => should give you the hierarchy you're looking for