在rails范围中排除id

NAR*_*KOZ 2 activerecord scope ruby-on-rails

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

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) }
Run Code Online (Sandbox Code Playgroud)

Ben*_*ret 5

试试这个:

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