Skip to content

Instantly share code, notes, and snippets.

@h-lame
Created January 12, 2010 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save h-lame/275076 to your computer and use it in GitHub Desktop.
Save h-lame/275076 to your computer and use it in GitHub Desktop.
# Assuming AR test models are present...
# The salient bits are:
#
# class Developer < ActiveRecord::Base
# has_many :audit_logs
# end
#
# class AuditLog < ActiveRecord::Base
# belongs_to :developer, :validate => true
# end
d = Developer.new :name => 'Murray'
d.audit_logs.build :message => 'What AR does'
d.audit_logs.first.developer #=> nil
d.valid? #=> true
d2 = Developer.new :name => 'Hans'
d2.audit_logs.build :message => 'Simulating what :inverse_of does', :developer => d2
d2.audit_logs.first.developer #=> d2
d2.valid? #=> BOOM! Stack Trace Too Deep
# It's even worse in reality as the Developer model has a :before_create that
# will populate audit_logs with the first AuditLog instance. You can't create or
# save any Developer instances!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment