Skip to content

Instantly share code, notes, and snippets.

@mateusg
Last active December 20, 2015 22:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mateusg/6202625 to your computer and use it in GitHub Desktop.
Save mateusg/6202625 to your computer and use it in GitHub Desktop.
# === Sem o inverse_of
c = Customer.first
o = c.orders.first
c.first_name == o.customer.first_name # => true
c.first_name = 'Manny'
c.first_name == o.customer.first_name # => false
# === Com o inverse_of
class Customer < ActiveRecord::Base
has_many :orders, inverse_of: :customer
end
class Order < ActiveRecord::Base
belongs_to :customer, inverse_of: :orders
end
c = Customer.first
o = c.orders.first
c.first_name == o.customer.first_name # => true
c.first_name = 'Manny'
c.first_name == o.customer.first_name # => true
# Fonte: http://guides.rubyonrails.org/association_basics.html#bi-directional-associations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment