Use a scope by default on a Rails has_many relationship

前端 未结 3 1041
难免孤独
难免孤独 2021-01-30 05:13

Let\'s say I have the following classes

class SolarSystem < ActiveRecord::Base
  has_many :planets
end

class Planet < ActiveRecord::Base
  scope :life_supp         


        
3条回答
  •  感动是毒
    2021-01-30 05:22

    In Rails 5, the following code works fine...

      class Order 
        scope :paid, -> { where status: %w[paid refunded] }
      end 
    
      class Store 
        has_many :paid_orders, -> { paid }, class_name: 'Order'
      end 
    

提交回复
热议问题