在rails范围中排除id

时间:2011-09-04 09:49:52

标签: ruby-on-rails activerecord scope

如何排除此范围内的某些项目:

scope :within_category, ->(category) { joins(:category).where(:categories => { :id => category }) }

像这样:

scope :within_category, ->(category, item_type) { joins(:category).where(:categories => { :id => category }, :id NOT IN item_type.id) }

1 个答案:

答案 0 :(得分:5)

试试这个:

scope :within_category, ->(category, item_type) { joins(:category).where(:categories => { :id => category }).where(self.arel_table[:id].not_in(item_type.id) }