Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

AR inverse_of doesn't work after creation of associated things.

View ar_inverse_of_test.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
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
View ar_inverse_of_test.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
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
Something went wrong with that request. Please try again.