Rails4 ActiveRecord has_one :conditions key removed

我与影子孤独终老i 提交于 2021-01-28 12:10:40

问题


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

I am getting this error:

      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)

Which is raised here:

      /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)>'
. . .

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:

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

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?

From Rails Upgrade Guide:

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')

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.


回答1:


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'


来源:https://stackoverflow.com/questions/28179670/rails4-activerecord-has-one-conditions-key-removed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!