Rails4 ActiveRecord has_one:删除了条件键

Rails4 ActiveRecord has_one :conditions key removed

我正在将 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)>'
. . .

现在,我查看了文档,果然,:conditions 在 Rails4.0.0 中作为 has_one 关联的键被删除,而它在 3.2.0 中存在。所以,我的问题是:

has_one :conditions => 键去哪儿了? 何时宣布弃用? 它的弃用在哪里宣布? 什么是替代品?

P.S。万一有人决定 has_one :conditions => 应该是 has_many :conditions =>,我尝试切换方法名称并得到相同的错误。现在边缘 rails 关联文档仍然将 :conditions 列为 has_many 的有效键,但我得到了同样的错误。怎么回事?

来自Rails升级指南:

Rails 4.0 has deprecated the old-style hash based finder API. This means that methods which previously accepted "finder options" no longer do. For example, Book.find(:all, conditions: { name: '1984' }) has been deprecated in favor of Book.where(name: '1984')

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

如果您查看 official guide to upgrading rails,就会提到弃用。一般来说,whereconditions 的替代品。它应该作为 lambda 传递。

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

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