We have the following model:
payment.rb:
class Payment < ActiveRecord::Base
has_many :currencies
default_scope :include => :currencies
end
We are using default_scope
to eager load the currencies. When we run this we get the following error:
[2012-03-22 21:43:25] ERROR NoMethodError: undefined method `each' for nil:NilClass
/home/me/.rvm/gems/jruby-1.6.7@my_project/gems/activerecord-3.2.1/lib/active_record/associations/preloader/association.rb:88:in `associated_records_by_owner'
org/jruby/RubyArray.java:1615:in `each'
/home/me/.rvm/gems/jruby-1.6.7@my_project/gems/activerecord-3.2.1/lib/active_record/associations/preloader/association.rb:85:in `associated_records_by_owner'
...
The problem can be found in:
Line 88 and 89 in association.rb (github link):
...
owner_key = record[association_key_name].to_s
owners_map[owner_key].each do |owner|
...
When I step through the code I can see that record[association_key_name]
is a BigDecimal e.g. 108.0, and when to_s
is applied it becomes "108.0". However, the keys in the owners_map
hash is expecting "108" not "108.0"
The association_key_name
is the name of the foreign key in the Oracle database and is defined as type Number. The primary key is also defined as type Number, but strangely enough it doesn't resolve to a BigDecimal.
Any ideas? Is this a possible bug? Or does the precision on the database column need altered?
System Info:
JRuby on Rails (3.2.1)
ActiveRecord JDBC Adapter
Oracle Database (using views instead of tables)
The problem stems from the fact that the foreign key on the database was defined as Number with precision. Thanks to tharrison for pointing me to this article.
I got our DBA to remove the precision on the field and everything worked fine. Of course, we are in the fortunate position of building a new product where our schema can be easily changed...