Skip to content

Instantly share code, notes, and snippets.

@kenguie
Last active August 29, 2015 14:07
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 kenguie/b9bd7b74e37d4f09005b to your computer and use it in GitHub Desktop.
Save kenguie/b9bd7b74e37d4f09005b to your computer and use it in GitHub Desktop.
class AddRelationshipTable < ActiveRecord::Migration
def change
create_table :relationships do |t|
t.integer :follower_id
t.integer :followed_id
end
end
end
class User < ActiveRecord::Base
def full_name
fname + " " + lname
end
def location
city + ", " + state
end
has_many :tweets
has_many :relationships, foreign_key: :follower_id, dependent: :destroy
has_many :followers, through: :relationships
has_many :followeds, through: :relationships
end
class Tweet < ActiveRecord::Base
belongs_to :user
end
class Relationship < ActiveRecord::Base
belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User"
validates_uniqueness_of :follower_id, scope: :followed_id
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment