【发布时间】:2012-11-09 04:41:52
【问题描述】:
我有一个 Rails 应用程序,其中包含三个模型,称为作者、书籍和作者身份。例如,一本书通过称为作者身份的联合模型有很多作者,而作者通过称为作者身份的联合模型有很多书
class Author < ActiveRecord::Base
attr_accessible :name
has_many :authorships
has_many :books, :through => :authorships
end
class Book < ActiveRecord::Base
attr_accessible :name, :author_ids
has_many :authorships
has_many :authors, :through => :authorships
end
class Authorship < ActiveRecord::Base
attr_accessible :book_id, :author_id
belongs_to :book
belongs_to :author
end
现在我的问题是,我怎样才能找到作为相似作者的任何选定的书籍
例如,<% book = Book.first %>
像
<% book.similar_authors.each do |book| %>
#......
<% end %>
我将使用什么样的查询来定义similar_authors
【问题讨论】:
-
是什么让一位作者与另一位作者相似?
-
某种意义上的相似作者?
-
作者 ID。这意味着查找具有相似 author_id 的书籍
-
显示与所选图书具有相同 author_id 的图书
标签: ruby-on-rails activerecord ruby-on-rails-3.1 ruby-on-rails-3.2