Skip to content

Instantly share code, notes, and snippets.

@r38y
Created February 18, 2012 03:03
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 r38y/1857117 to your computer and use it in GitHub Desktop.
Save r38y/1857117 to your computer and use it in GitHub Desktop.
# 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
@jamesarosen
Copy link

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 a user_id.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment