2011-09-09 8 views
1

Ruby on Rails 3.0.10 및 두 개의 ActiveRecord::Relation "엔터티"를 "뺍니다"그리고 다시 ActiveRecord::Relation을 가지고 싶습니다. ,ActiveRecord :: 관계 문제

@unchecked_articles = @articles - @articles_checked 

# $ @unchecked_articles.class 
# => Array 

@unchecked_articles.method_call 
# raise a NoMethodError error (read above for more information). 

위의 코드, 어쨌든, 루비 Array 그래서 "플레이"수없는 반환 (사용 where, order .. : 그건 내가 두 ActiveRecord::Relation 객체 (@articles@articles_checked) 다음과 같은 코드를 가지고있다 나는 더 이상 정상적으로 할 수있는 것과 마찬가지로 ActiveRecord::Relation을 사용합니다. 나는 위의 변경 한 후 내가 ActiveRecord::Relation 개체를 검색 할 수있는 방법

NoMethodError (undefined method 'method_call' for #<Array:0x000001063dd658>) 

: 콘솔에서 다음과 같은 오류가 발생합니다? 또는 더 나은 방법은 내가 @unchecked_articles으로 검색하고자하는 것을 달성하는 방법입니까?

답변

0

모델이 어떻게 보이는지 모르지만 기사의 범위를 정의하는 것이 좋습니다.

Article < ActiveRecord::Base 
    scope :unchecked, where(:checked => false) 

    #or this, apologies for somewhat unefficient, but you already seem to have several queries 
    scope :unchecked2, lambda { |checked| where(["id not in (?)", checked.map(&:id)]) } 

end 

Article.unchecked 
Article.unchecked2(@unchecked_articles) 
2

scope을 사용하십시오.

scope :unchecked, where("checked is FALSE") 

나는 이렇게 체크 된 기사에 플래그를 지정합니다. 범위를 정의한 후에는 Article.unchecked으로 전화하여 미확인 기사의 AR을 얻을 수 있습니다.