Skip to content

Commit

Permalink
Make a Relationship model
Browse files Browse the repository at this point in the history
  • Loading branch information
hogemax committed Mar 7, 2018
1 parent 384c054 commit 090bd61
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 1 deletion.
6 changes: 6 additions & 0 deletions app/models/relationship.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Relationship < ActiveRecord::Base
belongs_to :follower, class_name: "User"
belongs_to :followed, class_name: "User"
validates :follower_id, presence: true
validates :followed_id, presence: true
end
18 changes: 18 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,29 @@ class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
has_many :microposts, dependent: :destroy
has_many :relationships, foreign_key: "follower_id", dependent: :destroy
has_many :followed_users, through: :relationships, source: :followed
has_many :reverse_relationships, foreign_key: "followed_id",
class_name: "Relationship",
dependent: :destroy
has_many :followers, through: :reverse_relationships, source: :follower

def feed
Micropost.where("user_id = ?", id)
end

def following?(other_user)
relationships.find_by(followed_id: other_user.id)
end

def follow!(other_user)
relationships.create!(followed_id: other_user.id)
end

def unfollow!(other_user)
relationships.find_by(followed_id: other_user.id).destroy
end

devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable

Expand Down
13 changes: 13 additions & 0 deletions db/migrate/20180306140953_create_relationships.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateRelationships < ActiveRecord::Migration
def change
create_table :relationships do |t|
t.integer :follower_id
t.integer :followed_id

t.timestamps
end
add_index :relationships, :follower_id
add_index :relationships, :followed_id
add_index :relationships, [:follower_id, :followed_id], unique: true
end
end
13 changes: 12 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20180305154033) do
ActiveRecord::Schema.define(version: 20180306140953) do

create_table "microposts", force: true do |t|
t.string "content"
Expand All @@ -22,6 +22,17 @@

add_index "microposts", ["user_id", "created_at"], name: "index_microposts_on_user_id_and_created_at"

create_table "relationships", force: true do |t|
t.integer "follower_id"
t.integer "followed_id"
t.datetime "created_at"
t.datetime "updated_at"
end

add_index "relationships", ["followed_id"], name: "index_relationships_on_followed_id"
add_index "relationships", ["follower_id", "followed_id"], name: "index_relationships_on_follower_id_and_followed_id", unique: true
add_index "relationships", ["follower_id"], name: "index_relationships_on_follower_id"

create_table "users", force: true do |t|
t.string "name", default: "", null: false
t.string "email", default: "", null: false
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/relationships.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
follower_id: 1
followed_id: 1

two:
follower_id: 1
followed_id: 1
7 changes: 7 additions & 0 deletions test/models/relationship_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class RelationshipTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 090bd61

Please sign in to comment.