Created
February 18, 2012 03:03
-
-
Save r38y/1857117 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
# This is for an address book-like app. | |
# Do people ever associate nested objects with the very top object to | |
# Make sure that the nested object can never be seen by anybody besides | |
# the User/Owner? Basically, this is so I wouldn't have to go through | |
# person to find the owner of a piece of data. | |
class User < ActiveRecord::Base | |
has_many :people, foreign_key: :owner_id | |
has_many :addresses # thinking about adding this | |
end | |
class Person < ActiveRecord::Base | |
belongs_to :owner, class_name: 'User' | |
end | |
class Address < ActiveRecord::Base | |
belongs_to :person | |
belongs_to :owner, class_name: 'User' # and adding this | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This pattern has a further benefit: if you ever end up sharding your database (on
User
here), you're already nearly there since everything that belongs to the user will have auser_id
.