What is the equivalent of the has_many 'conditions' option in Rails 4?

后端 未结 5 1247
渐次进展
渐次进展 2020-12-02 12:11

Can someone tell me what is the equivalent way to do the following line in Rails 4?

has_many :friends, :through => :friendships, :conditions => \"statu         


        
5条回答
  •  余生分开走
    2020-12-02 12:39

    Needs to be the second arg:

    class Customer < ActiveRecord::Base
      has_many :orders, -> { where processed: true }
    end
    

    http://edgeguides.rubyonrails.org/association_basics.html#scopes-for-has-many

    RESPONSE TO UPDATE:

    Put the order inside the block:

    has_many :friends, -> { where(friendship: {status: 'accepted'}).order('first_name DESC') }, :through => :friendships
    

提交回复
热议问题