Has_many:通过_ids无法处理条件

时间:2011-04-06 18:11:24

标签: ruby-on-rails ruby-on-rails-3

假设我通过AuthorAssignment模型有许多作者的Post模型:

class Post < ActiveRecord::Base
    has_many :author_assignments, :dependent => :destroy
    has_many :authors, :through => :author_assignments
    has_many :featured_authors, :through => :author_assignments, :conditions => "`author_assignments`.featured = 1"
end

这是我的问题:

我打电话的时候:

@post.featured_author_ids

SQL查询不包括'author_assignments'.feature等于true的条件。

SELECT `author_assignments`.author_id FROM `author_assignments` WHERE (`author_assignments`.post_id = X)

另一方面,当我打电话时:

@post.featured_authors

条件包含在SQL中,预期结果是正确的。

思想?

1 个答案:

答案 0 :(得分:0)

它可能与你正在使用的数据库有关,即SQLite等(不是mysql)。 在这种情况下,尝试:

:conditions=>["author_assignments.featured = ?",true]
相关问题