Rails4 ActiveRecord has_one:条件键被删除

时间:2015-01-27 20:45:54

标签: ruby-on-rails-4 activerecord has-one

我正在将RoR3.2应用转换为v4.2。在某些模型中,我们有这种结构:

  # A correspondent can have only one currently active client role or
  # it may have none.
  has_one           :active_client,   :class_name => 'Client', 
                    :conditions => IsActiveRow

我收到此错误:

      Unknown key: :conditions. Valid keys are: :class_name, :class,
 :foreign_key, :validate, :autosave, :dependent, :primary_key,
 :inverse_of, :required, :as, :foreign_type
 (ActionView::Template::Error)

这里提到了:

      /home/byrnejb/Projects/Software/theHeart/code/proforma/libexec
/bundle/lib/ruby/2.2.0/gems/activesupport-4.2.0/lib/active_support
/core_ext/hash/keys.rb:75:in `block in assert_valid_keys'

      /home/byrnejb/Projects/Software/theHeart/code/proforma/libexec
/bundle/lib/ruby/2.2.0/gems/activesupport-4.2.0/lib/active_support
/core_ext/hash/keys.rb:73:in `each_key'

      /home/byrnejb/Projects/Software/theHeart/code/proforma/libexec
/bundle/lib/ruby/2.2.0/gems/activesupport-4.2.0/lib/active_support
/core_ext/hash/keys.rb:73:in `assert_valid_keys'

      /home/byrnejb/Projects/Software/theHeart/code/proforma/libexec
/bundle/lib/ruby/2.2.0/gems/activerecord-4.2.0/lib
/active_record/associations/builder/association.rb:82:in `validate_options'

      /home/byrnejb/Projects/Software/theHeart/code/proforma/libexec
/bundle/lib/ruby/2.2.0/gems/activerecord-4.2.0/lib
/active_record/associations/builder/association.rb:62:in `initialize'

      /home/byrnejb/Projects/Software/theHeart/code/proforma/libexec
/bundle/lib/ruby/2.2.0/gems/activerecord-4.2.0/lib
/active_record/associations/builder/association.rb:47:in `new'

      /home/byrnejb/Projects/Software/theHeart/code/proforma/libexec
/bundle/lib/ruby/2.2.0/gems/activerecord-4.2.0/lib/active_record
/associations/builder/association.rb:47:in `create_builder'

      /home/byrnejb/Projects/Software/theHeart/code/proforma/libexec
/bundle/lib/ruby/2.2.0/gems/activerecord-4.2.0/lib
/active_record/associations/builder/association.rb:35:in `build'

      /home/byrnejb/Projects/Software/theHeart/code/proforma/libexec/
bundle/lib/ruby/2.2.0/gems/activerecord-4.2.0/lib/
active_record/associations.rb:1385:in `has_one'

      /home/byrnejb/Projects/Software/theHeart/code/proforma/app/models
/orm_active_record/correspondent.rb:14:in `<class:Correspondent>'

      /home/byrnejb/Projects/Software/theHeart/code/proforma/app/models
/orm_active_record/correspondent.rb:1:in `<top (required)>'
. . .

现在,我查看了文档,并且确实如此:条件被移除作为Rails4.0.0中has_one关联的关键,而它存在于3.2.0中。所以,我的问题是:

has_one:conditions =&gt;在哪里?关键去? 什么时候宣布弃用? 它的弃用在哪里宣布? 什么是替代品?

P.S。如果有人决定has_one :conditions =>应该has_many :conditions =>,我尝试切换方法名称并得到相同的错误。现在,边缘轨道关联文档仍将:conditions列为has_many的有效密钥,但我得到了相同的错误。发生了什么事?

来自Rails升级指南:

  

Rails 4.0已弃用旧式基于散列的finder API。这个   表示以前接受&#34; finder选项&#34;没有   做得更久。例如,Book.find(:all,conditions:{name:&#39; 1984&#39;})   已被弃用,以支持Book.where(名称:&#39; 1984&#39;)

据我所知,这是唯一提及任何地方的弃用。没有提及我可以在3.2.13的ActiveRecord文档中找到:conditions仍然存在的地方,并且没有提到4.0.2中:conditions已经消失的变化

1 个答案:

答案 0 :(得分:6)

如果你看一下official guide to upgrading rails,就会提到弃用。通常,whereconditions的替代品。它应该作为lambda传递。

在您的情况下,它看起来像:

has_one :active_client, -> { where IsActiveRow }, :class_name => 'Client'