2012-11-08 5 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에 추가합니다. 내가 처음으로 사용자 캠페인의 미가공 게시물을 찾아야합니다. Smth like this :연관성없이 연관성없이 모두 찾으십시오.

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

답변

0

MrYoshoshi, Thanks a lot! 너는 include에 대해 나를 기억한다. join을 시도하기 전에 INNER JOIN 때문에 문제가 해결되지 않아 인스턴스 메서드로 문제를 해결했습니다.

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