mar*_*ion 0 ruby-on-rails ruby-on-rails-4
当我尝试只做一个普通的操作时,即使有它应该作用的对象/记录,它find_each也会不断返回。nil
[28] pry(main)> Node.find_each do |node|
[28] pry(main)* end
Node Load (1.0ms) SELECT "nodes".* FROM "nodes" ORDER BY "nodes"."id" ASC LIMIT 1000
=> nil
[29] pry(main)> Node.all.find_each do |node|
[29] pry(main)* node
[29] pry(main)* end
Node Load (0.7ms) SELECT "nodes".* FROM "nodes" ORDER BY "nodes"."id" ASC LIMIT 1000
=> nil
[31] pry(main)> Node.count
(0.5ms) SELECT COUNT(*) FROM "nodes"
=> 2
[32] pry(main)> Node.find_each do |node|
[32] pry(main)* puts node
[32] pry(main)* end
Node Load (0.5ms) SELECT "nodes".* FROM "nodes" ORDER BY "nodes"."id" ASC LIMIT 1000
#<Node:0x007fd49ffbc680>
#<Node:0x007fd49ffbc388>
=> nil
Run Code Online (Sandbox Code Playgroud)
我如何find_each像那样简单地输出该模型的所有对象Node.all,为什么它只在我执行 a 时才输出每个节点的 AR 对象的 ID puts node?
[35] pry(main)> Node.all
Node Load (0.4ms) SELECT "nodes".* FROM "nodes"
=> [#<Node id: 85, name: "House Fire 2", family_tree_id: 57, user_id: 57, media_id: 228, media_type: "Video", created_at: "2015-05-15 00:20:26", updated_at: "2015-05-20 01:06:34", circa: nil, is_comment: nil, cached_votes_total: 0, cached_votes_score: 0, cached_votes_up: 0, cached_votes_down: 0, cached_weighted_score: 0, cached_weighted_total: 0, cached_weighted_average: 0.0>,
#<Node id: 86, name: "10PP Form Video", family_tree_id: 57, user_id: 57, media_id: 229, media_type: "Video", created_at: "2015-05-15 01:26:28", updated_at: "2015-05-22 20:35:58", circa: nil, is_comment: nil, cached_votes_total: 1, cached_votes_score: 1, cached_votes_up: 1, cached_votes_down: 0, cached_weighted_score: 1, cached_weighted_total: 1, cached_weighted_average: 0.0>]
Run Code Online (Sandbox Code Playgroud)
> Node.find_each do |node|
> end
=> nil
Run Code Online (Sandbox Code Playgroud)
在本例中,nil 来自该块的定义;不是 的结果Node.find_each。
尝试打印块中的内容,如下所示:
> Node.find_each do |node|
> puts node.id
> end
Run Code Online (Sandbox Code Playgroud)
这将打印出 2 个 id。
打印出所有对象:
> Node.find_each do |node|
> puts node.inspect
> end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
365 次 |
| 最近记录: |