如何使用`scope`查找模型的所有记录?

时间:2011-02-19 18:01:32

标签: ruby-on-rails-3

我有:

scope :all_somethings, lambda { find(:all) }

但是这会返回一个数组(而不是ActiveRecord::Relation),所以如果我在其上调用order,我会收到一个错误:

@somethings = current_user.somethings.all_somethings.order("created_at desc")

undefined method `order' for #<Array:0xb6ebedc2>

1 个答案:

答案 0 :(得分:2)

你为什么不这样做

@everything = MyModel.all

您应该只能删除行中的“all_somethings”:

@somethings = current_user.somethings.order("created_at desc")