2016-08-03 24 views
0

我正在使用rails web应用程序,并且收到ActiveRecord关系,但是我需要访问数组中的所有对象。有没有一种简单的方法来实现这一点,因为我找不到任何帮助在线的东西。我收到了ActiveRecord关系,但是我需要访问数组中的对象。

这是ActiveRecord返回与它内的对象,但我需要与它的所有对象的数组。干杯

[#<ActiveRecord::Relation [#<Chapter id: 995, description: "hello world", created_at: "2016-08-03 09:43:50", updated_at: "2016-08-03 09:43:50", adventure_id: 787, parent_choice_id: 672>]>]

回答

0

.to_a是解决方案。 (http://edgeapi.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-to_a

但你真的应该保持关系,因为你可以使用像find,where和其他东西的方法。

但ActiveRecord :: Relation几乎响应了数组所做的所有事情。像each如下:

# assume that @objects is an ActiveRecord::Relation 
@objects.each do |obj| 
    puts obj.id 
end 

编辑: 我刚才看到你的关系是一个数组里面。你知道吗?

相关问题