在rails 3.1中使用has_many关联添加INNER JOIN

时间:2014-03-12 07:23:45

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

我在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。范围也是一种选择,但可以相关联地添加。

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

class User < ActiveRecord::Base
  has_many :likes, conditions: {"likes.likable_type = 'Post'", "posts.is_published IS true"}, dependent: :destroy
end