Bug of the Day: nil.include error with create_time_zone_conversion_attribute
This was an 'orrible bug. Tired and midway through writing up a Ruby on Rails tutorial on setting up emailing with AR Mailer and a little postfix configuration I wasn't exactly in the mood for a bizzarely cryptic issue to waste my time. Enter the mysterious create_time_zone_conversion_attribute? method in all it's glory. Basically you will get this error if you set ...
config.cache_classes = false
... wherever it is set in your environment files (in my case config/development.rb) but the error goes away if you change it to ...
config.cache_classes = true
... bloody daft! Here is the bug on lighthouse. Arrgh!!!!
Anyway here is the full stack trace which may help someone googling to pick up this page
NoMethodError in UsersController#create
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.include?
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/attribute_methods.rb:142:in `create_time_zone_conversion_attribute?'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/attribute_methods.rb:75:in `define_attribute_methods'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/attribute_methods.rb:71:in `each'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/attribute_methods.rb:71:in `define_attribute_methods'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/attribute_methods.rb:350:in `respond_to?'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/base.rb:2587:in `attributes='
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/base.rb:2583:in `each'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/base.rb:2583:in `attributes='
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/base.rb:2283:in `initialize'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/base.rb:691:in `new'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.2.2/lib/active_record/base.rb:691:in `create'
vendor/gems/ar_mailer-1.3.1/lib/action_mailer/ar_mailer.rb:92:in `perform_delivery_activerecord'
vendor/gems/ar_mailer-1.3.1/lib/action_mailer/ar_mailer.rb:91:in `each'
vendor/gems/ar_mailer-1.3.1/lib/action_mailer/ar_mailer.rb:91:in `perform_delivery_activerecord'
/usr/lib/ruby/gems/1.8/gems/actionmailer-2.2.2/lib/action_mailer/base.rb:526:in `__send__'
/usr/lib/ruby/gems/1.8/gems/actionmailer-2.2.2/lib/action_mailer/base.rb:526:in `deliver!'
/usr/lib/ruby/gems/1.8/gems/actionmailer-2.2.2/lib/action_mailer/base.rb:392:in `method_missing'
app/controllers/users_controller.rb:47:in `create'
app/controllers/users_controller.rb:45:in `create'
Happy hackin'
January 28th, 2009 - 02:47
I had the same bug but needed a different solution. I was actually downgrading off edge to 2.2.0 so maybe my situation was different. This did the trick for me: http://www.railsformers.com/article/activerecord-timezone-settings-bug
February 4th, 2009 - 00:34
Thanks a lot for this post – that is a horrible and very opaque bug and I don’t think I would have solved that without your help!! Your solution works perfectly although of course that also means that I now have to remember to restart the server after every change to the code.
February 11th, 2009 - 14:41
I ran into this bug several times. Most recently for a route (with a path_prefix) that should have worked fine, but threw this error. Your solution fixed the problem, but as Frank said, requires constant server restarts in development mode
March 6th, 2009 - 01:17
Thanks for your work-around advice.
August 18th, 2009 - 01:23
This is fixed by changing this line in your environment.rb (or anywhere else you have it):
config.time_zone = ‘UTC’
to:
config.active_record.default_timezone = :utc
(but it then gives me a Stack Level Too Deep, but I guess that’s my problem…)
August 18th, 2009 - 18:02
Thanks!
I have been banging my head against this for hours. Stef, the time zone configuration fixed the problem for me. Thanks for posting this and for posting the error output so it was easily searchable!
August 31st, 2009 - 11:52
Thank’s a lot!
September 8th, 2009 - 00:35
I downgraded to 2.3.2 from 2.3.3, and it solved the problem for me.
September 23rd, 2009 - 03:26
I toast your bloggerisation of this problem!
October 16th, 2009 - 21:12
Thanks for blogging this, thanks to your post I fixed this quickly, I’m sure this coulda cost me hours!
October 31st, 2009 - 16:32
I cant thank you enough for this. This was a really nasty one.
Like KendallB – I too was on the verge of downgrading from 2.3.3.
November 11th, 2009 - 06:27
Thank you so much!
November 23rd, 2009 - 06:02
Thanks both (the original poster and Stef) for the solution.
I was headbanging since two hours ago!
January 20th, 2010 - 03:03
oh man, I had no idea why my tests were all of a sudden failing. I was using the console in test mode and didn’t want to have to restart every time i made a change. Thanks SOO much! I spent 2 hours of messing around before i decided to google this
February 18th, 2010 - 07:07
Thanks Stef! Your solution fixed it for me.
April 7th, 2010 - 08:02
Argh, what a PIA! Thanks, this saved my A….
August 4th, 2010 - 11:57
Thanks for the blog post saved me some headbanging too. I’ve upgraded from rails 2.1 and had:
config.time_zone = ‘Wellington’
(I’m in NZ)
Removing that line makes Time.zone.now fail so the following two lines seems to work for and keeps it consistent with the old behaviour
config.time_zone = ‘Wellington’
config.active_record.default_timezone = :utc
October 7th, 2010 - 18:39
Thanks, you save my day, dude.
July 24th, 2011 - 09:21
I fixed it like this
if ['2.3.5','2.3.6','2.3.7','2.3.8','2.3.9','2.3.11', '2.3.12'].include?(Rails.version.to_s)
require ‘active_record’
module ActiveRecord
module AttributeMethods
module ClassMethods
def create_time_zone_conversion_attribute?(name, column)
time_zone_aware_attributes && !((skip_time_zone_conversion_for_attributes || []).include?(name.to_sym)) && [:datetime, :timestamp].include?(column.type)
end
end
end
end
end