'inflections dont work after update of activerecord
I have updated active record from 3.2.12 to 4.1.4. Accordingly I have update activesupport also from 3.2.12 to 4.1.4. Now when I do data base queries (h2-data base) I cannot use
mrs = MeasurementResultSample.find_by_duration_and_measurement_result_id(duration, id)
I can oly use
mrs = MeasurementResultSample.where(:duration => duration, :measurement_result_id => id).first
The activerecord-jdbch2-adapter I have upgraded from 1.2.9 to 1.3.9
How can I make my 'find_by' methods using one or more fields working again?
Update: actually it turned out, that find_by is working but I had to check if the table exsist with the method exists?. What wont work is find_last_by... or find_all_by... or find_first_by... .
Solution 1:[1]
As I understand it, Rails is moving away from certain 'magic' method names due to the poor performance of 'method_missing,' upon which they're based.
However, find_by works. So you can re-work your query with:
mrs = MeasurementResultSample.find_by(duration: duration, measurement_result_id:, id)
Amusingly, the implementation behind the scenes is:
def find_by(*args)
where(*args).take
end
http://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-find_by
Edit:
There's a gem that includes Rails 4 deprecated finders:
https://github.com/rails/activerecord-deprecated_finders
Confusingly it states that find_by... methods aren't deprecated. However, if you've updated ActiveRecord outside Rails it might help.
If not, you could always look at the Rails 3 ActiveRecord code and port the relevant methods across.
Solution 2:[2]
Thanks to A Fader Darkly! I finally started to port the code to activerecord 4.1.4. I opted to change the finder methods to where(...).
Here I found some information too what-s-new-in-active-record-rails-4. It's the cleanest way. I noticed, that I had to use the exist? method in some cases to make sure the table is not nil. I did not have to do this before (in 3.2.12) with inflections used.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | user2025166 |
