LoginSignup
2
2

More than 5 years have passed since last update.

[Rails4.0] DEPRECATION WARNING への対応

Posted at

ActiveRecord関連で

Rails4で3系の書き方をしていたら、以下のようなwarningが発生した。

DEPRECATION WARNING: Relation#calculate with finder options is deprecated. Please build a scope and then call calculate on it instead. (called from hoge_check at /work/Fuga/app/models/tag.rb:103)

DEPRECATION WARNING: The :distinct option for `Relation#count` is deprecated. Please use `Relation#distinct` instead. (eg. `relation.distinct.count`). (called from hoge_check at /work/Fuga/app/models/tag.rb:103)

対処

countの中に条件(condition)を書くのではなく、
条件はwhere、カウントはcount、と分離して書けばok。

改善前
TagHistory.count(:conditions => {:action_id => @params.aid})
改善後
TagHistory.where(action_id: @params.aid).count()
改善前
TagHistory.count(:conditions => {:tag_id => self.id, :user_id => @user.id})
改善後
TagHistory.where(tag_id: self.id, user_id: @user.id).count()

解決。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2