Last active
August 29, 2015 14:07
-
-
Save kenguie/b9bd7b74e37d4f09005b to your computer and use it in GitHub Desktop.
This file contains 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 AddRelationshipTable < ActiveRecord::Migration | |
def change | |
create_table :relationships do |t| | |
t.integer :follower_id | |
t.integer :followed_id | |
end | |
end | |
end |
This file contains 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 | |
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