Skip to content

Instantly share code, notes, and snippets.

@rcook
Last active December 29, 2015 12:19
Show Gist options
  • Save rcook/7670071 to your computer and use it in GitHub Desktop.
Save rcook/7670071 to your computer and use it in GitHub Desktop.
Test case for Rails/Active Record bug 13044 (https://github.com/rails/rails/issues/13044) Note that this only reproduces using the "postgresql" database adapter. Prerequisites: local instance of PostgreSQL with database name "bug", user name "bug" and password "bug".
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', '=3.2.14'
gem 'pg'
GEMFILE
system 'bundle'
end
require 'bundler'
Bundler.setup(:default)
require 'active_record'
require 'minitest/autorun'
require 'logger'
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(
:adapter => 'postgresql',
:host => 'localhost',
:database => 'bug',
:username => 'bug',
:password => 'bug'
)
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
drop_table :clients
drop_table :sessions
drop_table :client_session_assignments
create_table :clients do |t|
t.string :name
end
create_table :sessions do |t|
t.string :name
end
create_table :client_session_assignments do |t|
t.belongs_to :client
t.belongs_to :session
t.decimal :score
end
end
class Client < ActiveRecord::Base
has_many :client_session_assignments
has_many :sessions,
:through => :client_session_assignments,
:select => 'sessions.*, client_session_assignments.score'
end
class Session < ActiveRecord::Base
has_many :client_session_assignments
has_many :clients,
:through => :client_session_assignments,
:select => 'clients.*, client_session_assignments.score'
end
class ClientSessionAssignment < ActiveRecord::Base
belongs_to :client
belongs_to :session
end
class BugTest < ActiveSupport::TestCase
def test_association_stuff
client = Client.create!(:name => 'the-client')
session = Session.create!(:name => 'the-session')
assignment = ClientSessionAssignment.create!(:client => client, :session => session, :score => 123.4)
assert_equal BigDecimal, assignment.score.class
retrieved_client = Client.find(client.id)
# BUG: This assert fails since the PostgreSQL database
# adapter returns 'String' instead of the expected 'BigDecimal'.
assert_equal BigDecimal, retrieved_client.sessions.first.score.class
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment