2

I have 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.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Beer Brother
  • 514
  • 2
  • 6
  • 15

2 Answers2

9

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)
Ju Nogueira
  • 8,435
  • 2
  • 29
  • 33
2
Article.all(:joins => :sections, 
  :conditions => [ 'sections.id in ? and sections.id not in ?', 
  [3, 4, 6, 7, 8, 9], @some_ids ], :limit => 4)

untested

Jonathan Julian
  • 12,163
  • 2
  • 42
  • 48