给定一组“书籍”,找到所有“作者”(不重复)的最佳方法是什么?
所以假设我们有一个经典的关联:
class Author < ActiveRecord::Base
has_many :books
end
class Book < ActiveRecord::Base
belongs_to :author
end
我现在的做法是这样的:
@books = Book.where("some condition")
@authors = Author.where(:id => @books.map(&:author_id))
有没有更好的方法呢?