参照サイトを見てフォロー機能を実装してましたが。このサイトの「rails コンソールから確認してみる。」以降なんですけど参照サイトのように@userが@user2をフォローしている状態になりません。データーベースの問題なのでしょうか?
[1] pry(main)> @user = User.find(1) User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] => #<User id: 1, email: "test@gmail.com", created_at: "2019-05-24 20:22:42", updated_at: "2019-05-29 09:19:05", name: "鳥", profile_photo: nil, s_introduction: "新米エンジニアです"> [2] pry(main)> @user2 = User.find(2) User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]] => #<User id: 2, email: "test1@gmail.com", created_at: "2019-05-25 20:18:45", updated_at: "2019-05-27 11:30:12", name: "鳥愛好家", profile_photo: nil, s_introduction: nil> [3] pry(main)> @user.follow(@user2) (0.1ms) begin transaction Relationship Create (2.8ms) INSERT INTO "relationships" ("followed_id", "created_at", "updated_at") VALUES (?, ?, ?) [["followed_id", 2], ["created_at", "2019-06-06 06:54:12.967703"], ["updated_at", "2019-06-06 06:54:12.967703"]] (1.8ms) commit transaction User Load (0.2ms) SELECT "users".* FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."followed_id" WHERE "relationships"."followed_id" = ? [["followed_id", 1]] => [#<User id: 2, email: "test1@gmail.com", created_at: "2019-05-25 20:18:45", updated_at: "2019-05-27 11:30:12", name: "鳥愛好家", profile_photo: nil, s_introduction: nil>] [4] pry(main)> @user.following.count (0.7ms) SELECT COUNT(*) FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."followed_id" WHERE "relationships"."followed_id" = ? [["followed_id", 1]] => 0 [5] pry(main)>
Relationhipsのテーブルをrails:dbで確認しました。
$ rails db SQLite version 3.19.3 2017-06-27 16:48:08 Enter ".help" for usage hints. sqlite> .table ar_internal_metadata photos schema_migrations comments posts users likes relationships sqlite> .schema relationsships sqlite> .schema relationships CREATE TABLE IF NOT EXISTS "relationships" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "follower_id" integer, "followed_id" integer, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL); CREATE INDEX "index_relationships_on_follower_id" ON "relationships" ("follower_id"); CREATE INDEX "index_relationships_on_followed_id" ON "relationships" ("followed_id"); CREATE UNIQUE INDEX "index_relationships_on_follower_id_and_followed_id" ON "relationships" ("follower_id", "followed_id"); sqlite>
テーブルの構成も異常がないように思えますがなんで回答者さんが指摘して下さったようにfollower_idが入っていないんでしょうか?
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/07 04:42
2019/06/07 05:50