开发者

Exclude some ids from result in Rails ActiveRecord

I have 开发者_Python百科following statement for query articles from some sections

Article.all(:joins => :sections, :conditions => { :sections =>{ :id => [3, 4, 6, 7, 8, 9] }, :id_not_in => @some_ids  }, :limit => 4)

Variable @some_ids is array with ids of articles which must be excluded from result.


If Article has_many :sections, try:

Article.find(:all, :joins => :sections, :conditions => ["sections.id IN (?) AND
   id NOT IN (?)", [1,2,3], @some_ids], :limit => 4)


Article.all(:joins => :sections, 
  :conditions => [ 'sections.id in ? and sections.id not in ?', 
  [3, 4, 6, 7, 8, 9], @some_ids ], :limit => 4)

untested

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜