在我的 Rails 应用程序中,我编写了一个从数据库中的一组记录生成唯一名称数组的方法:
class Book < ActiveRecord::Base
attr_accessible :title, :author
def self.all_authors
self.uniq.pluck(:author)
end
end
此方法按预期工作,但此应用程序最终可能会有大量作者,所以现在我想在控制器中对这个查询进行分页。
class AuthorsController < ApplicationController
def index
@authors = Book.all_authors.limit(10).offset(params[:page]*10)
end
end
显然,这不起作用,因为pluck(:authors)
返回一个数组而不是一个ActiveRecord::Relation
. 是否有替代方法pluck
可以让我使用 Arel 方法调用链?或者也许是一种让 pluck 返回一个ActiveRecord::Relation
而不是一个数组的方法?