Complex JOIN with ActiveRecord and Rails 3

前端 未结 2 1383
不思量自难忘°
不思量自难忘° 2021-02-06 03:55

I have the following models:

class User < ActiveRecord::Base
  has_many :memberships
  has_many :groups, :through => :memberships
end

class Group < Act         


        
2条回答
  •  灰色年华
    2021-02-06 04:49

    something like this should work for you, although it requires mixing in a little raw SQL

    Post
      .joins("INNER JOIN memberships ON memberships.group_id = posts.group_id")
      .where(:memberships => {:user_id => current_user.id})
    

提交回复
热议问题