Created
January 23, 2011 22:55
-
-
Save rsutphin/792546 to your computer and use it in GitHub Desktop.
Odd problem with decimal PKs and validation failures in ActiveRecord's save! on JRuby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Gemfile.lock | |
| *.sqlite3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| require 'rubygems' | |
| require 'active_record' | |
| ActiveRecord::Base.establish_connection( | |
| :database => 'people.sqlite3', | |
| :adapter => RUBY_PLATFORM == 'java' ? 'jdbcsqlite3' : 'sqlite3' | |
| ) | |
| ActiveRecord::Schema.create_table('people', | |
| :id => false, | |
| :force => true | |
| ) do |t| | |
| t.decimal :id, :primary_key => true | |
| t.string :username | |
| end | |
| ActiveRecord::Base.connection.execute( | |
| "INSERT INTO people (id, username) VALUES (1.0, 'jo')") | |
| class Person < ActiveRecord::Base | |
| validate :validate_fail | |
| def validate_fail | |
| errors.add(:username, "no good") | |
| end | |
| end | |
| p = Person.find_by_username 'jo' | |
| # This block throws the expected exception on MRI 1.8 and 1.9, but | |
| # throws a NoMethodError referencing "coerce" on TrueClass on JRuby. | |
| begin | |
| p.save! | |
| rescue ActiveRecord::RecordInvalid => e | |
| puts "Validation aborted save as expected.\n #{e.class}: #{e}" | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| source :rubygems | |
| gem 'activerecord', '2.3.10' | |
| platform :jruby do | |
| gem 'activerecord-jdbcsqlite3-adapter' | |
| end | |
| platform :ruby_18, :ruby_19 do | |
| gem 'sqlite3' | |
| end |
Author
Author
Output on JRuby 1.6.0.RC1:
$ ruby bizarre-jruby-ar.rb
-- create_table("people", {:id=>false, :force=>true})
-> 0.1110s
-> 0 rows
org/jruby/RubyBigDecimal.java:821:in `eql_p': undefined method `coerce' for true:TrueClass (NoMethodError)
from /Users/rsutphin/.rvm/gems/jruby-1.6.0.RC1@tmp/gems/activerecord-2.3.10/lib/active_record/base.rb:2981:in `convert_number_column_value'
from /Users/rsutphin/.rvm/gems/jruby-1.6.0.RC1@tmp/gems/activerecord-2.3.10/lib/active_record/attribute_methods.rb:311:in `write_attribute'
from /Users/rsutphin/.rvm/gems/jruby-1.6.0.RC1@tmp/gems/activerecord-2.3.10/lib/active_record/dirty.rb:139:in `write_attribute_with_dirty'
from /Users/rsutphin/.rvm/gems/jruby-1.6.0.RC1@tmp/gems/activerecord-2.3.10/lib/active_record/base.rb:2551:in `id='
from /Users/rsutphin/.rvm/gems/jruby-1.6.0.RC1@tmp/gems/activerecord-2.3.10/lib/active_record/transactions.rb:212:in `rollback_active_record_state!'
from /Users/rsutphin/.rvm/gems/jruby-1.6.0.RC1@tmp/gems/activerecord-2.3.10/lib/active_record/transactions.rb:200:in `save_with_transactions!'
from bizarre-jruby-ar.rb:35:in `__file__'
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output on JRuby 1.5.6:
-- create_table("people", {:id=>false, :force=>true})
-> 0.1080s
-> 0 rows
/Users/rsutphin/.rvm/gems/jruby-1.5.6@bcsec/gems/activerecord-2.3.10/lib/active_record/base.rb:2981:in
convert_number_column_value': undefined methodcoerce' for true:TrueClass (NoMethodError)from /Users/rsutphin/.rvm/gems/jruby-1.5.6@bcsec/gems/activerecord-2.3.10/lib/active_record/attribute_methods.rb:311:in
write_attribute' from /Users/rsutphin/.rvm/gems/jruby-1.5.6@bcsec/gems/activerecord-2.3.10/lib/active_record/dirty.rb:139:inwrite_attribute_with_dirty'from /Users/rsutphin/.rvm/gems/jruby-1.5.6@bcsec/gems/activerecord-2.3.10/lib/active_record/base.rb:2551:in
id=' from /Users/rsutphin/.rvm/gems/jruby-1.5.6@bcsec/gems/activerecord-2.3.10/lib/active_record/transactions.rb:212:inrollback_active_record_state!'from /Users/rsutphin/.rvm/gems/jruby-1.5.6@bcsec/gems/activerecord-2.3.10/lib/active_record/transactions.rb:200:in `save_with_transactions!'
from bizarre-jruby-ar.rb:35
The output on ruby 1.8 and 1.9 is as expected.