2012-09-20 7 views
0

모든 기사에 대해 인벤토리를 정의하려고하지만 매개 변수가있는 기사를 제외하고 싶습니다. 여기범위 예외 규칙

def self.by_not_tags(tag) 
    joins(:tags).where('tags.title != ?', tag) 
end 

내보기에 호출 방법은 다음과 같습니다 : 여기

Article 
    has_many :tags, through: :articletags 
    ArticleTags 
    belongs_to :article 
    belongs_to :tags 
    Tags 
    has_many :article, through: articletags 

내 모델의 태그없이 하나를 정의하는 방법입니다 : 여기

처럼 관계가 모습입니다

<%= link_to (tag.title), articles_path(:scope => tag.title) %> 

내 컨트롤러는 다음과 같습니다.

def custom 
    if params[:scope].nil? 
     @articles = Article.all(:order => 'created_at DESC') 
    else 
     @articles = Article.by_tags(params[:scope]) 
     @articles2 = Article.by_not_tags(params[:scope]) 
    end 
    end 

목표는 태그가있는 모든 기사를 먼저 본 다음 해당 태그없이 다른 기사를 표시하여 중복되지 않도록하는 것입니다.

내 문제는 조인이지만 태그가없는 기사를 찾는 방법을 모르겠습니다. 어쩌면 예외가 작동하지만, 어떤 종류의 쿼리가 작동하는지 모르겠습니다. 내가 검증 위에서 언급 있다고 가정하지 않는 경우

답변

2

,

Article.where('article_tag_id is null') 

, ArticleTag 모델은 article_id를하고 tag_id 모두의 존재를 검증 할 필요가 가정

Article.where('not exists (select 1 from article_tags where article_id = articles.id)') 
관련 문제