Skip to content
Create a gist now

Instantly share code, notes, and snippets.

# 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
class Person < ActiveRecord::Base
belongs_to :owner, class_name: 'User'
class Address < ActiveRecord::Base
belongs_to :person
belongs_to :owner, class_name: 'User' # and adding this

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
Something went wrong with that request. Please try again.