public
Created

  • Download Gist
whatevs.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# 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

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.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.