Navigation Menu

Skip to content

Commit

Permalink
Calling ActiveRecord#inspect on an unloaded association won't wipe th…
Browse files Browse the repository at this point in the history
…e collection [#9 state:resolved]

Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information
zargony authored and josh committed May 8, 2008
1 parent 2561732 commit bcb090c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
Expand Up @@ -118,7 +118,7 @@ def target=(target)
end

def inspect
reload unless loaded?
load_target
@target.inspect
end

Expand Down Expand Up @@ -167,7 +167,7 @@ def merge_options_from_reflection!(options)
def with_scope(*args, &block)
@reflection.klass.send :with_scope, *args, &block
end

private
def method_missing(method, *args)
if load_target
Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/cases/associations_test.rb
Expand Up @@ -149,6 +149,12 @@ def test_save_on_parent_does_not_load_target
assert !david.projects.loaded?
end

def test_inspect_does_not_reload_a_not_yet_loaded_target
andreas = Developer.new :name => 'Andreas', :log => 'new developer added'
assert !andreas.audit_logs.loaded?
assert_match(/message: "new developer added"/, andreas.audit_logs.inspect)
end

def test_save_on_parent_saves_children
developer = Developer.create :name => "Bryan", :salary => 50_000
assert_equal 1, developer.reload.audit_logs.size
Expand Down
4 changes: 4 additions & 0 deletions activerecord/test/models/developer.rb
Expand Up @@ -49,6 +49,10 @@ def find_least_recent
before_create do |developer|
developer.audit_logs.build :message => "Computer created"
end

def log=(message)
audit_logs.build :message => message
end
end

class AuditLog < ActiveRecord::Base
Expand Down

0 comments on commit bcb090c

Please sign in to comment.