undefined method `find_all_by_X'

347 views Asked by At

Currently maintaining some old Ruby server and got the following error in Log:

NoMethodError (undefined method `find_all_by_X_ID' for #<Class:0x00000005555555>):
  app/controllers/some_controller.rb:10:in `buggy_function'

When viewed the faulty line in code of the buggy function is looks like this:

Hash[S.find_all_by_X_ID(TaskRun.select(:x_id).uniq.where(y_id: @y.Y_ID).map(&:x_id)).map { |s| [s.S_IDENTIFIER, s.X_ID] }]

To be frank, I'm new to Ruby, and wondering how implementing this find_all_by_X query would be the best, and why it appears as it should be automatic (as it has to do with the model component).

We're working on Ruby version 2.

1

There are 1 answers

7
max pleaner On BEST ANSWER

It seems that find_all_by was deprecated in Rails 4 ...

Internally Rails implemented methods like find_all_by_x_id using method_missing (the method is actually defined dynamically through metaprogramming) ... but you don't have to worry about that for your use-case.

In terms of your code, if we extract the x_ids list into a variable:

x_ids = TaskRun.select(:x_id).uniq.where(y_id: @y.Y_ID).map(&:x_id)

Then you have this line that you need to rewrite:

S.find_all_by_X_ID(x_ids)

You can rewrite this as:

S.where(x_id: x_ids)

See https://stackoverflow.com/a/23921890/2981429