我试图通过数组中的索引使用 ActiveRecord 选择对象集合。
我知道我可以选择 Item.first 或 Item.last 或单个或范围。但我想通过数组中位置的索引更新任意组。
collection = Item.all.to_a
collection[3,5,9,11]
这可能吗?
提前致谢...
-- 编辑--
感谢 tokland 的帮助,我能够让它完美地工作。
如果其他人想做类似的事情,这就是我所做的:
yesterday = Time.now - 1.day
i = Item.all
new_items = i.values_at(1,3,5,10,11,14,18)
new_items.each{ |e| e.update_attributes(:published_at => yesterday) }
最佳答案
如果您指的是数组中的索引(不是 ID):
collection.values_at(3, 5, 9, 11)
关于ruby-on-rails - 在 Rails 3 中按数组索引选择对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7894801/