2014-02-18 75 views
0

聲明:如何使用範圍塊重寫has_many?

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

得到了一個警告:

棄用警告:在你的User.has_many 以下選項:read_access_mappings聲明中棄用::條件。請 改爲使用範圍塊。

我該如何用新語法重寫它?

回答

0

我認爲最好的解決辦法:

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 
0

也許你可以試試這個:

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

Source