Rails ActiveRecord conditions

后端 未结 4 1245
误落风尘
误落风尘 2020-12-15 11:26

Is there a way to create a condition like this?

@products = Product.find(:all,
  :limit => 5,
  :conditions => { :products => { :locale => \'en\'         


        
4条回答
  •  -上瘾入骨i
    2020-12-15 11:55

    Rails 3.2.9


    Controller

      @products = Product.english_but_not(1).with_tags('a','b').limit(5)
    

    Model

    class Product < ActiveRecord::Base
      attr_accessible :locale
      has_many :tags
      scope :english, -> { where(:locale => 'en') }
      scope :except_id, ->(id) { where(arel_table[:id].not_eq(id)) }
      scope :english_but_not, ->(id) { english.except_id(id) }
      scope :with_tags, ->(*names) { includes(:tags).where(:tags => {:name => names}) }
    end
    

提交回复
热议问题