Skip to content

Instantly share code, notes, and snippets.

@alloy
Forked from h-lame/gist:275076
Created January 12, 2010 15:05
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 alloy/275261 to your computer and use it in GitHub Desktop.
Save alloy/275261 to your computer and use it in GitHub Desktop.
##
#
# Automatic
#
class Developer < ActiveRecord::Base
has_many :audit_logs # the macro checks if it can find an obvious inverse_of, but none is found
end
class AuditLog < ActiveRecord::Base
belongs_to :developer, :validate => true # the macro checks if it can find an obvious inverse_of, which it does and sets :inverse_of on both reflections
end
# At this point, the :inverse_of options should already have been inferred.
Developer.reflect_on_association(:audit_logs).options[:inverse_of] # => :developer
AuditLog.reflect_on_association(:developer).options[:inverse_of] # => :audit_logs
##
#
# Explicit
#
class Developer < ActiveRecord::Base
has_many :audit_logs, :inverse_of => :developer_with_logs
end
class AuditLog < ActiveRecord::Base
belongs_to :developer, :inverse_of => nil # Does *not* have an inverse assocaition.
belongs_to :developer_with_logs, :inverse_of => :audit_logs
end
Developer.reflect_on_association(:audit_logs).options[:inverse_of] # => :developer_with_logs
AuditLog.reflect_on_association(:developer).options[:inverse_of] # => nil
AuditLog.reflect_on_association(:developer_with_logs).options[:inverse_of] # => :audit_logs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment