Skip to content

Instantly share code, notes, and snippets.

@benbabics
Last active August 29, 2015 14:17
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 benbabics/2c1573b3a4ac87eb1053 to your computer and use it in GitHub Desktop.
Save benbabics/2c1573b3a4ac87eb1053 to your computer and use it in GitHub Desktop.
Rails API
###
possible schema
###
User
has_many :posts
has_many :tools
has_many :products, through: :tools
Post
belongs_to :user
has_many :tools
has_many :products, through: :tools
Tool
has_many :users
has_many :posts
###
pseudo-code
###
user # => { id: 1, name: "Ben Babics" }
user.posts # => [{ id: 3, name: "How To Fix Leaky Pipes", user_id: 1 }]
user.products # => [{ id: 5, name: "Hammer", user_id: 1, post_id: 3 }]
user.posts.first # => { id: 3, name: "How To Fix Leaky Pipes", user_id: 1 }
user.posts.first.products # => [{ id: 5, name: "Hammer", user_id: 1, post_id: 3 }]
post.products # => [{ id: 5, name: "Hammer", user_id: 1, post_id: 3 }]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment