Rails4 ActiveRecord has_one:条件键被删除

Jam*_*rne 1 activerecord has-one ruby-on-rails-4

我正在将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
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

      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)
Run Code Online (Sandbox Code Playgroud)

这是在这里提出的:

      /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)>'
. . .
Run Code Online (Sandbox Code Playgroud)

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

has_one:conditions => key在哪里?什么时候宣布弃用?它的弃用在哪里宣布?什么是替代品?

PS关于某人决定has_one :conditions =>应该是has_many :conditions => 我尝试切换方法名称并获得相同错误的机会.现在边缘轨道关联文档仍然:conditions列为有效密钥has_many,但我得到相同的错误.到底是怎么回事?

从Rails升级指南:

Rails 4.0已经弃用了旧式的基于散列的finder API.这意味着之前接受"查找器选项"的方法不再适用.例如,Book.find(:all,conditions:{name:'1984'})已被弃用,以支持Book.where(名称:'1984')

据我所知,这是唯一提到任何地方的弃用.没有提到我可以在ActiveRecord3.2.13 的文档中找到:conditions它仍然存在的地方,并且没有提到4.0.2中的变化,其中:conditions就没有了.

ptd*_*ptd 6

如果您查看升级rails官方指南,则会提到弃用.一般来说,where是替代conditions.它应该作为lambda传递.

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

has_one :active_client, -> { where IsActiveRow }, :class_name => 'Client'
Run Code Online (Sandbox Code Playgroud)