Skip to content

Instantly share code, notes, and snippets.

@phiggins
Last active December 16, 2015 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phiggins/5478294 to your computer and use it in GitHub Desktop.
Save phiggins/5478294 to your computer and use it in GitHub Desktop.
AR inverse_of doesn't work after creation of associated things.
require 'active_record'
require 'sqlite3'
require 'minitest/autorun'
puts "ActiveRecord: #{ActiveRecord::VERSION::STRING}"
ActiveRecord::Base.establish_connection(
:adapter => "sqlite3",
:database => ":memory:"
)
ActiveRecord::Schema.define do
create_table :users, force: true do |t|
end
create_table :preferences, force: true do |t|
t.integer :user_id
end
end
class User < ActiveRecord::Base
has_one :preferences, :inverse_of => :user
end
class Preferences < ActiveRecord::Base
belongs_to :user, :inverse_of => :preferences
end
describe "User/Preferences relationship" do
before do
@user = User.create
@user.create_preferences
end
it "is the same object after creation" do
assert_equal @user.object_id, @user.preferences.user.object_id
end
it "is the same object after reloading" do
@user.reload
assert_equal @user.object_id, @user.preferences.user.object_id
end
end
ActiveRecord: 3.2.13
-- create_table(:users, {:force=>true})
-> 0.0096s
-- create_table(:preferences, {:force=>true})
-> 0.0004s
Run options: --seed 27668
# Running tests:
F.
Finished tests in 0.040778s, 49.0461 tests/s, 49.0461 assertions/s.
1) Failure:
test_0001_is_the_same_object_after_creation(User/Preferences relationship) [ar_inverse_of_test.rb:36]:
Expected: 19526680
Actual: 18232740
2 tests, 2 assertions, 1 failures, 0 errors, 0 skips
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment