Created
December 24, 2013 22:30
-
-
Save davetapley/8118411 to your computer and use it in GitHub Desktop.
Code for Bi-directional associations in Rails 4.
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
# Activate the gem you are reporting the issue against. | |
gem 'activerecord', '4.0.0' | |
require 'active_record' | |
require 'minitest/autorun' | |
require 'logger' | |
# This connection will do for database-independent bug reports. | |
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
ActiveRecord::Schema.define do | |
create_table :dungeons do |t| | |
t.integer :level, default: 1 | |
end | |
create_table :traps do |t| | |
t.references :dungeon | |
end | |
end | |
class Dungeon < ActiveRecord::Base | |
has_many :traps, inverse_of: :dungeon | |
end | |
class Trap < ActiveRecord::Base | |
belongs_to :dungeon, inverse_of: :traps | |
end | |
class BugTest < Minitest::Unit::TestCase | |
def test_association_stuff | |
d = Dungeon.create | |
t = d.traps.create | |
assert_equal d.level, t.dungeon.level | |
d.level = 10 | |
assert_equal d.level, t.dungeon.level | |
end | |
end |
It seems that instead of d.level = 10
on line 35, you can use any of the following and everything works as given by the first statement:
d.level = 10
t.dungeon.level = 10
d.traps.first.dungeon.level = 10
t_.dungeon.level = 10
I've also tested with these extra assertions before the assignment (line 35):
t_ = d.traps.first
assert_equal d.level, 1
assert_equal t.dungeon.level, 1
assert_equal t_.dungeon.level, 1
And these after:
assert_equal d.level, 10
assert_equal t.dungeon.level, 10
assert_equal t_.dungeon.level, 10
In all cases I can't find any evidence to support the third bullet point:
for belongs_to associations has_many inverse associations are ignored.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The documentation in question is here.
They give this snippet, which confirms the first statement:
But then the third bullet point below states:
Which seems to be inconsistent with the first (correct) statement?