我在rails 3.1中有关联
has_many :likes, :dependent => :destroy
我想在上述关联
中添加一个条件的连接likes = user.likes.joins('INNER JOIN posts ON posts.id = likes.likable_id and likes.likable_type = "Post"').where("posts.is_published", true)
所以不要使用user.likes.joins()。我只想使用user.likes。范围也是一种选择,但可以相关联地添加。
答案 0 :(得分:0)
尝试这样的事情:
class User < ActiveRecord::Base
has_many :likes, conditions: {"likes.likable_type = 'Post'", "posts.is_published IS true"}, dependent: :destroy
end