I'm getting deprecation errors on an upgrade to rails 4.2.1, Modifying already cached Relation. The cache will be reset. Use a cloned Relation to prevent this warning.
The action I'm trying to run gets number of users by month who have logged in.
My test is just simply:
get :page
expect(response).to be_success
Controller action:
def page
@months = {}
(0..11).each do |month|
@months[month] = User.group(:usergroup).number_first_logged_in(Date.new(Date.today.year,month+1, 1))
end
end
User model
class Model < ActiveRecord::Base
...
def number_first_logged_in(month)
where(first_logged_in_at: month.beginning_of_month..month.end_of_month).count
end
end
I realize that I'm running almost the same query 12 times but with different parameters. This method is used elsewhere when the users are not grouped. How can I 'clone' a relation as suggested in the deprecation warning?
I don't want to simply ignore this as it's filling up my screen whilst running tests, which is not very helpful
In my case, it was the squeel gem which produces the deprecation warning. A simple monkeypatch fixes the warning.
I'm not sure if it breaks squeel behaviour, but it works for me. It looks like to be a problem with Rails 4.2.x in combination with squeel. I also pushed this into the squeel issue tracker https://github.com/activerecord-hackery/squeel/issues/374.