Skip to content

Instantly share code, notes, and snippets.

@tyleramos
Created October 12, 2018 13:58
Show Gist options
  • Save tyleramos/50ccff7a3f75048e5651989b82f21a8e to your computer and use it in GitHub Desktop.
Save tyleramos/50ccff7a3f75048e5651989b82f21a8e to your computer and use it in GitHub Desktop.
# Org has the following columns:
# - id
# - name
class Org < ApplicationRecord
has_many :users
end
# User has the following columns:
# - id
# - first_name
# - last_name
# - email
# - organization_id
class User < ApplicationRecord
has_many :preferences
end
# UserPreference has the following columns:
# - id
# - user_id
# - name
class UserPreference < ApplicationRecord
belongs_to :user
end
Organization.first.users.each do |u|
puts "#{u.name} - #{u.preference.name}"
end
# I want to see a list of users with their preferences that looks like this:
# Forrest Gump - Chocolates
# Forrest Gump - Running
# Dorothy Gale - Toto
# Dorothy Gale - Home
# Darth Vadar - Dark Side
# Would this code work? Why?
# How would you improve it?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment