Ruby on rails 如何使用作用域块重写has\u many?

Ruby on rails 如何使用作用域块重写has\u many?,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,宣言: has_many :read_access_mappings, :primary_key => "username", :foreign_key => "username", :class_name => 'Mapping', :conditions => {"mappings.read_access" => true} 收到警告: 弃用警告:您的User.h

宣言:

  has_many :read_access_mappings, 
           :primary_key => "username", 
           :foreign_key => "username", 
           :class_name => 'Mapping',
           :conditions => {"mappings.read_access" => true}
收到警告:

弃用警告:您的User.has\u中的以下选项有很多 :read\u access\u映射声明已弃用::条件。请 改为使用范围块


如何用新语法重写它?

我认为最好的解决方案是:

class Cls
 has_many :readable_mappings,
          -> { readable },
          primary_key: 'username',
          foreign_key: 'username',
          class_name: 'Mapping'
end

class Mapping
  scope :readable, where(read_access: true)
end

cls_instance.readable_mappings

也许你可以试试这个:

has_many :read_access_mappings, -> { where read_access: true },class_name: 'Mapping',
           primary_key => "username", 
           foreign_key => "username"