Exclude some ids from result in Rails ActiveRecord

好久不见. 提交于 2020-01-11 10:54:14

问题


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 wich must be excluded from result.


回答1:


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)



回答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



来源:https://stackoverflow.com/questions/2580980/exclude-some-ids-from-result-in-rails-activerecord

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!