NoMethodError using order intermittently - ruby-on-rails

I am getting the following error in a production rails application intermittently
NoMethodError (undefined method `values' on priority:Symbol.):
app/controllers/things_controller.rb:33:in `index'
The offending line looks like this:
#things = Thing.where(:some_column => 'some_value').order(:priority).reverse
Thing is an activerecord model.
The weird thing is, when I restart the application the error disappears. It is only under some strange set of circumstances that this happens (which I can't reproduce in preprod/dev environments).
Has anyone come across something like this before? Can anyone suggest how I would go about diagnosing this bug? The line in question doesn't seem to be the problem (the logs in production also don't show the full stacktrace)

After a length hair pulling session, I discovered that this is due to a bug in rubinius (2.2.10, and 2.2.9). The ActiveRecord query methods where and order both call enumerable#grep internally. After the application has been running for some time, or some unknown conditions are met, this function stops behaving correctly when the array contains symbols.
When a block is given as an argument to the grep function, this block will always be applied to the symbol elements of the array, regardless of the pattern given.
Bug report is here

Related

Rails jbuilder error: ActionView::Template::Error (undefined method

I have a Rails 6 app and we're using jbuilder to define the shape of JSON responses.
Recently, I started getting the following error:
ActionView::Template::Error (undefined method `empty?' for #<Account:0x0000000116743030>):
The stack trace points to this block inside a jbuilder file:
json.account do
json.call(#account, *Account::APP_FIELDS)
json.logo_url #account.logo
end
If I comment out both of the lines inside the block, the error goes away. If I remove either of the lines and leave the other, the error returns. The stack trace just points me to the first line of the block.
What's going on? How do I fix this?
Figured it out!
Higher in the jbuilder file, a partial is included that adds the same account key. This was causing some kind of name collision.
I stumbled on this by temporarily testing with my key renamed accountt and suddenly it worked. That's when I realized it was conflicting name that was causing the issue. I didn't see it because it was in a different file and the error message was confusing. If your jbuilder file is complicated and you think you're running into this same issue, this is an easy way to test.

How can I get Rails to give me more details on an error it's not logging?

This has been stumping me: Rails is throwing this error, after it's finished rendering my views, but before it gets back to the browser:
ActionView::Template::Error (undefined method `start_with?' for #<Proc:0x00005651bfe017f0>)
And... that's it. There's no stack trace. I get shown the standard 500 "We're sorry, but something went wrong" page, despite having config.consider_all_requests_local = true set. There are no further details either in the terminal or in log/development.log.
I can't find any Procs that it might be complaining about, nor can I find any calls to start_with? that might be the cause; I've gone back through Git history and isolated the issue to one commit (this one, if you want to take a look in detail), but nothing within that commit jumps out as being obvious.
Calling a render layout: false does work, as does simplifying my layouts/application.js down to just a <%= yield %>, which makes me think it might be something in there, however - I made no changes to it or any views at all in the commit in which the issue appeared.
What I'd really like to know is how I can get Rails to give me the stack trace for this error, so I can figure out where it's coming from. If you have any ideas where the bug itself might be, those are more than welcome too.
Drop this in an initializer (proc.rb):
class Proc
def start_with?(*args)
puts caller
end
end

Now that ActiveRecord::Base.silence {} is gone, how do I bring back that functionality?

I do a lot of spatial queries that dump massive amounts of text in the form of logs every time I run queries. These slow down my programs enormously.
I'm being forced to update my rails to '4.1.2' from '4.0.0' and ActiveRecord::Base.silence has been completely deprecated as in, it doesn't work. Here's what used to work
ActiveRecord::Base.silence do
noisy_query
end
When I try this now, I get this error....
ArgumentError: wrong number of arguments (0 for 1)
from /Users/davidddouglas/.rvm/gems/ruby-1.9.3-p551/gems/activesupport-4.1.2/lib/active_support/core_ext/kernel/reporting.rb:82:in `capture'
In 4.0.0 it sent a deprecation warning, and now the script just doesn't work. Oddly enough, the function is still declared, it just doesn't work anymore and expects some kind of parameter. I've tried passing in nil and got this error:
NoMethodError: undefined method `reopen' for nil:NilClass
I'm looking for a way to monkeypatch the old functionality back into my program to get my scripts to work again. Not too worried about best practices as this is an application I'm using internally with little to no front end and 0 users other than myself.
Thanks
silence moved to a core extension on logger.
From their example,
logger = Logger.new("log/development.log")
logger.silence(Logger::INFO) do
logger.debug("In space, no one can hear you scream.")
logger.info("Scream all you want, small mailman!")
end

NoMethodError undefined method '-#' NoMethodError in Controller Ruby on Rails

Context:
I pulled the most recent code from the repository and tried to make sure that the changes I was about to push up were going to work with that version of the code. This is a Ruby on Rails application. Also worth noting is the fact that when running the main application that I pulled from on the web, this error does not show up. But if I run my branch or the main branch cloned onto my environment, the error always shows up for every url I try. So it is on my end.
Problem:
As soon as I go to localhost:3000, I get the following error:
NoMethodError in HomeController#index
undefined method `-#' for #<ActionDispatch::Response:0x64fd460>
What I've Tried:
I have asked my question on the #rubyonrails IRC channel and nobody was able to determine what was going on through the Full Trace (I haven't posted it here because I wasn't sure what was the best way to do that on here; it didn't look very good in the code block or block quote). I have looked at my HomeController's index method, which is defined as such:
def index
#groups = #current_user.groups
#things = Thing.where(:group_id => #groups.map{|e|e.id})
end
I have also Googled around and haven't found what I need to fix the problem.
What I've Learned So Far:
-# is an operator. Some people may receive a similar error in assuming that Ruby has the shortcut to
variable = variable + 1
that a lot of other languages have:
variable++
Here is an example of that case: Undefined method `+#' for false:FalseClass (NoMethodError) ruby
Question:
Does anyone have any further suggestions on how to find the issue here? Also, if I could easily put the Full Trace on here, formatted in an aesthetically pleasing manner, would someone tell me how? I'm at a loss with this one :(
Update (2/8/2013):
It seems that the issue does not necessarily reside in the HomeController nor home/index.html.erb View. I have attempted to access ANY url with a valid action and the same error occurs with "NoMethodError in..." changing to the corresponding [...]Controller#index.
Update (2/9/2013):
Since this error happens no matter what url I try to navigate to, I decided to look in the routes.rb file in the config folder. I ran my server through rubymine instead of the command line this time, which made it a little easier to read for me. I started looking through all the spit out and I noticed an interested line that consisted of:
["private-key looking thing"] [127.0.0.1] Started GET "/" for 127.0.0.1 at 2013-02-09 18:20:52 -0700
It seems like there is a syntactical error in routes.rb (that's my best guess at this point). This does not explain why this only is an issue on my local environment with the same code sets, but what else do I have to go off of?
Does anyone have any suggested things to be on the look out for while I sift through this file? Not really sure what to be looking for as far as errors are concerned. Rubymines inspection stuff converted all my double quotes to single quotes and doesn't really have anything else to complain about.
Thanks in advance,
Jake Smith
I am guessing it might as well be an syntactical error in the corresponding view page Home/index.html.haml .. I am suspecting there is unintended '-' in front of variable call. I tried to simulate a similar scenario in my rails platform and see following page on browser
undefined method `-#' for false:FalseClass
Correct lines of code
%h1 All Movies
= "filtervalue=#{#isFilterOld}"
= "Sortvalue=#{#isSortOld}"
Edited to simulate the error (observe the - in front of isFilterOld variable)
%h1 All Movies
= "filtervalue=#{-#isFilterOld}"
= "Sortvalue=#{#isSortOld}"
I have fixed the issue!
What fixed it:
Go to the directory where your gems are (for me that was C:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1)
Delete all gems except for bundler
Make sure you delete the gems from the /cache/, /gems/, and /specifications/ folders (I just deleted them from the /gems/ folder at first and bundle install indicated that it could still find the gems)
Run bundle install
Further Inquiry:
Does anybody have any idea why this worked? I don't know if at this point I can narrow down which gem was causing the issue because the app is working now (I can visit all the urls with corresponding views). If the issue comes up again, I will delete gems one by one to nail down which one was at least causing the issue for me. But if anyone has any insight on this, a more detailed answer would be greatly appreciated by many more people than just me, I think. Thanks to all who helped thus far!

Undefined Method errors on basic AR and AS methods when running threaded with Sidekiq on Heroku

I am getting a couple different errors at a particular line of code in one of my models when running in Sidekiq-queued jobs. The code in question is:
#lookup_fields[:asin] ||= self.book_lookups.find_by_name("asin").try(:value)
I either get undefined method 'scope' for #<ActiveRecord::Associations::AssociationScope:0x00000005f20cb0> or undefined method 'aliased_table_for' for #<ActiveRecord::Associations::AliasTracker:0x00000005bc3f90>.
At another line of code in another Sidekiq job, I get the error undefined method 'decrypt_and_verify' for #<ActiveSupport::MessageEncryptor:0x00000007143208>.
All of these errors make no sense, as they are standard methods of the Rails runtime support libraries.
The model in question has a :has_many association defined for the "book_lookups" model, "name" and "value" are fields in the "book_lookups" model. This always happens on the first 1-3 records processed. If I run the same code outside of a Sidekiq job, these errors do not occur.
I cannot reproduce the error on my development machine, only on production which is hosted at Heroku.
I may have "solved" the first set of errors by putting the code `BookLookup.new()' in an initializer, forcing the model to load before Sidekiq creates any threads. Only one night's work to go on, so we'll have to see if the trend continues...
Even if this solves the immediate problem, I don't think it solves the real underlying issue, which is classes not getting fully loaded before being used. Is class-loading an atomic operation? Is it possible for one thread to start loading a class and another to start using the class before it is fully loaded?
I believe that I have discovered the answer: config.threadsafe!, which I had not done. I have now done that and most if not all of the errors have disappeared. References: http://guides.rubyonrails.org/configuring.html, http://m.onkey.org/thread-safety-for-your-rails (especially the section "Ruby's require is not atomic").

Resources