Rails4 ActiveRecord has_one :conditions 键已删除 [英] Rails4 ActiveRecord has_one :conditions key removed

查看:17
本文介绍了Rails4 ActiveRecord has_one :conditions 键已删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am converting a RoR3.2 app to v4.2. In some models we have this construct:

  # 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 中存在.所以,我的问题是:

Now, I looked at the docs and, sure enough, :conditions is removed as a key for has_one associations in Rails4.0.0 while it is present in 3.2.0. So, my questions are:

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

Where did the has_one :conditions => key go? When was its deprecation announced? Where is its deprecation announced? What is the replacement?

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

P.S. On the off chance that somebody decided that has_one :conditions => should be has_many :conditions => I tried switching the method name and got the same error. Now the edge rails associations documents still list :conditions as a valid key for has_many and yet I get the same error. What is going on?

来自 Rails 升级指南:

From Rails Upgrade Guide:

Rails 4.0 已弃用旧式基于哈希的查找器 API.这个意味着以前接受查找程序​​选项"的方法没有再做.例如 Book.find(:all, conditions: { name: '1984' })已弃用 Book.where(name: '1984')

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 完全消失了.

As far as I can determine this is the only mention of the deprecation anywhere. There is no mention that I could find in the ActiveRecord documentation for 3.2.13 where :conditions is still present and there is no mention of the change in 4.0.2 where :conditions is simply gone.

推荐答案

如果您查看 官方指南升级 rails 提到了弃用.通常,whereconditions 的替代品.它应该作为 lambda 传递.

If you look at the official guide to upgrading rails the deprecation is mentioned. In general, where is the replacement for conditions. It should be passed as a lambda.

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

In your case, it would look like:

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

这篇关于Rails4 ActiveRecord has_one :conditions 键已删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆