undefined method `find_all_by_X' - ruby-on-rails

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.

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

Related

Unable to use active record time methods inside ruby on rails 5.0 model

I'm currently unable to use any of the active support time methods inside my ruby on rails 5.0 model like the following:
5.seconds
2.days
10.minutes
throws an error:
NoMethodError: undefined method `seconds' for AS::Duration:0x007f97a5903b90 #value=5, #parts=[[:seconds, 5]] Did you mean? send
EDIT: here is the actual code causing an issue.
ReminderJob.set(wait: 5.seconds).perform_later(self.user.id)
Even tho I can see people using the below code fine and it works
UserReminderJob.set(wait: 1.week).perform_later user
However, it works in my console and in my controllers and views.
The error message states that the object is AS::Duration:0x007f97a5903b90, NOT an integer -- therefore the example of 5.seconds will not reproduce the problem.
This is also unusual, since 5.seconds will normally return an ActiveSupport::Duration object, not AS::Duration.
I would therefore hazard a guess that you're actually using the as-duration ruby gem rather than built-in rails behaviour. This extends the the Integer class in a different way, and returns an object that doesn't behave like an integer.
I think that an actual reproduction of your error could be achieved with: 5.seconds.seconds. In standard rails, this works fine (and returns the same value as 5.seconds), since ActiveSupport::Duration instances behave like Integers. But with this gem, it fails with the above error.

Monkey patching a db model class in Rails with Mongoid causes weird behaviour

I am using a development script file to check out new possible ideas. Recently I tried to monkey patch MyDBObject from within that script file.
Assume an empty dev.rb file and add a monkey patch right in the top like so:
class MyDBObject
def test_function
'function works'
end
end
Starting up the pry console and loading the file yields random results.
First I received:
NoMethodError: undefined method `relations' for MyDBObject:Class
Later the script loaded, but I couldn't access the original class any longer:
undefined method `first' for MyDBObject:Class
I noticed that prepending the line:
MyDBObject
right before the monkey patching, the intended functionality is achieved.
This appears to be some sort of lazy loading of the class objects. Can somebody cast some light on this for me please?
Depending on the order in which source files are loaded, you'll either be redefining the entire class, or having your changes replaced.
I highly recommend giving this a read: http://www.justinweiss.com/articles/3-ways-to-monkey-patch-without-making-a-mess/ (TLDR - put your patch in a module and explicitly include it)

rails dot notation no longer works for accessing hash values gives NoMethodError

I'm using rails 5 with ruby 2.3.3 . Today I added a gem, there was a version conflict so I took the gem out. Since then dot notation such as hash.test no longer works. It gives NoMethodError: private method test called for {:test=>"value"}:Hash
How can I access hashes with dot notation again?
Whatever you're using to use dot-notation to access a hash is probably using method_missing to trap your dot-notation method calls. But everything has a test method because Kernel#test exists and everything includes Kernel; also, pretty much everything in Kernel is private because Kernel is where methods go that we want to pretend are functions. For example:
> 'pancakes'.test
NoMethodError: private method `test' called for "pancakes":String
I suspect that you problem is your choice of :test as hash key.

Ruby on Rails: NoMethodError undefined method `get_yaml' for String

I tried to deploy a non public project but sometimes when tried to use
report pages (based on prawn) I have this problem:
NoMethodError (undefined method `get_yaml' for #<String:0x7fdecffd3738>):
The piece of code is:
report_content = report.draw(report_content.get_yaml)
Someone know: where are get_yaml come from?
Thanks.
The method #get_yaml is not a part of of the standard Ruby library, although #to_yaml is defined if the yaml library has been loaded. #get_yaml, if it exists, is defined by the application or by some library that the application is loading.

updating this static page controller code to a newer rails version

I've been trying to use the code here:
http://snafu.diarrhea.ch/blog/article/4-serving-static-content-with-rails
but I'm getting errors like:
undefined method `template_exists?' for #<StaticController:0xb74cbe4c>
How can I update this method to Rails 2.5? Probably there are other deprecated things too.
The method call template_exists? is deprecated as of Rails 2.2.1 (see: Rails APIdock)
A little bit of digging revealed the solution, by the original author, in the following article article. Basically requires adding the method into the StaticController, derived from ApplicationController and thus gives the method.

Resources