0

我有两个模型,书籍和目录。

class Book < ActiveRecord::Base
   has_and_belongs_to_many :catalogs
end

class Catalog < ActiveRecord::Base
   has_and_belongs_to_many :books
end

我需要查找属于特定目录列表的书籍。我努力了,

DEFAULT_CATALOGS = ["A","B"]
Book.joins(:catalogs).where("catalogname = ?",DEFAULT_CATALOGS)

但我得到一个异常 Mysql2::Error: Operand should contain 1 column(s):

4

1 回答 1

0

它应该是:

Book.joins(:catalogs).where("catalogs.catalogname IN (?)", DEFAULT_CATALOGS)

或者:

Book.joins(:catalogs).where(:catalogs => {:catalogname => DEFAULT_CATALOGS})
于 2012-08-15T13:10:34.277 回答