Last active
March 2, 2016 16:52
-
-
Save chrisdpeters/e7ae0197218651b14571 to your computer and use it in GitHub Desktop.
Users, Contacts, and Friendships
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Contact < ActiveRecord::Base | |
has_many :users | |
has_many :friendships | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This links up a user's contacts | |
class Friendship < ActiveRecord::Base | |
belongs_to :user | |
belongs_to :contact | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
belongs_to :contact # The user's own contact details | |
has_many :friendships # Joins up user's friends | |
has_many :friends, through: :friendships # Queries for user's friends | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UsersController < ApplicationController | |
def show | |
# This grabs both the user's contact details (:contact) and friends (:friends) | |
@user = User.includes(:contact, :friends).find(params[:key]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment