2012-11-08 59 views
0
class Campaign < ActiveRecord::Base 
    has_many :posts 
    has_many :users 
end 

class Post < ActiveRecord::Base 
    has_and_belongs_to_many :users 
    belongs_to :campaign 
end 

class User < ActiveRecord::Base 
    has_and_belongs_to_many :posts, :join_table => "users_posts" 
    belongs_to :campaign 
end 

如果用戶看到這篇文章,我將他添加到post.users。我需要查找用戶活動的第一個未看到的帖子。如下所示:找到所有無關聯通過協會

current_user.campaign.posts.where('posts.users not include current_user') 

回答

0

MrYoshiji,非常感謝!你記得我關於include。在我嘗試join之前,由於INNER JOIN因爲無法使用,所以我使用實例方法解決了我的問題:

def unwatched_post 
    campaign.posts.includes(:users).where('users.id != ? OR users.id IS NULL', self.id).first 
    end