diff --git a/app/models/relationship.rb b/app/models/relationship.rb new file mode 100644 index 0000000..f900f25 --- /dev/null +++ b/app/models/relationship.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 9116e39..a058100 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/db/migrate/20180306140953_create_relationships.rb b/db/migrate/20180306140953_create_relationships.rb new file mode 100644 index 0000000..fe36b5a --- /dev/null +++ b/db/migrate/20180306140953_create_relationships.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 05d866c..4bfea76 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -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 diff --git a/test/fixtures/relationships.yml b/test/fixtures/relationships.yml new file mode 100644 index 0000000..ad542c1 --- /dev/null +++ b/test/fixtures/relationships.yml @@ -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 diff --git a/test/models/relationship_test.rb b/test/models/relationship_test.rb new file mode 100644 index 0000000..700cc41 --- /dev/null +++ b/test/models/relationship_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class RelationshipTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end