-
-
Save alloy/275261 to your computer and use it in GitHub Desktop.
This file contains 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
## | |
# | |
# 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