テーブルは下記の通り。
ruby
1create_table "reactions", force: :cascade do |t| 2 t.integer "to_photo_id", null: false 3 t.integer "from_user_id", null: false 4 t.integer "status", null: false 5 t.datetime "created_at", null: false 6 t.datetime "updated_at", null: false 7 end
現状のデータベースは下記の通り。
sqlite
1id|to_photo_id|from_user_id|status|created_at|updated_at 2115|20|4|0|2020-01-24 16:47:34.029729|2020-01-24 16:47:34.029729 3★116|19|4|1|2020-01-24 16:47:35.470992|2020-01-24 16:47:35.470992 4117|18|4|0|2020-01-24 16:47:43.841167|2020-01-24 16:47:43.841167 5★118|17|4|1|2020-01-24 16:47:48.417951|2020-01-24 16:47:48.417951 6119|16|4|0|2020-01-24 16:52:36.698128|2020-01-24 16:52:36.698128 7120|15|4|0|2020-01-24 16:52:37.322925|2020-01-24 16:52:37.322925 8121|14|4|0|2020-01-24 16:52:37.857629|2020-01-24 16:52:37.857629 9122|13|4|0|2020-01-24 16:52:38.355283|2020-01-24 16:52:38.355283 10123|12|4|0|2020-01-24 16:52:38.841234|2020-01-24 16:52:38.841234 11124|11|4|0|2020-01-24 16:52:39.537211|2020-01-24 16:52:39.537211 12125|10|4|0|2020-01-24 16:52:40.085418|2020-01-24 16:52:40.085418 13126|9|4|0|2020-01-24 16:52:40.537790|2020-01-24 16:52:40.537790 14127|8|4|0|2020-01-24 16:52:40.952935|2020-01-24 16:52:40.952935 15128|6|4|0|2020-01-24 16:52:41.351016|2020-01-24 16:52:41.351016 16129|5|4|0|2020-01-24 16:52:41.867081|2020-01-24 16:52:41.867081 17130|19|2|0|2020-01-24 16:53:50.398350|2020-01-24 16:53:50.398350 18131|17|2|0|2020-01-24 16:54:05.624367|2020-01-24 16:57:43.336454 19132|7|2|0|2020-01-24 16:54:06.208412|2020-01-24 16:54:06.208412 20133|4|2|0|2020-01-24 16:54:07.498411|2020-01-24 16:54:07.498411 21134|1|2|0|2020-01-24 16:54:29.081213|2020-01-24 16:54:29.081213
やれたいこと
『status 0 な Reaction が1つもついていない to_photo_id』を選び出すことです!
一度statusのところにlike(0)のついたto_photo_idは抽出したくないのですが、
現状上記星印で示すように過去にdislike(1)がついたto_photo_idが出力されてしまっています。
表題の通り、あるカラムにおいて重複がある場合は最新のもののみ抽出対象とできらば行けそうな気がしているのですが調べても芳しいものが見当たらず。お手数おかけしますがよろしくお願いします。
現状、コントローラーは下記の通り。
ruby
1 @photos = photo.left_outer_joins(:reactions).where.not(photos: { user_id: current_user.id}).where("reactions.from_user_id != ? or reactions.from_user_id is null",current_user.id).where(reactions: { status: [1,nil]})
発生している問題・エラーメッセージ
特にエラーではないですが、ターミナルに流れているものを掲載しておきます。
Started POST "/reactions" for ::1 at 2020-01-25 11:28:24 +0900
Processing by ReactionsController#create as /
Parameters: {"photo_id"=>"17", "reaction"=>"like"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" IS NULL LIMIT ? [["LIMIT", 1]]
↳ app/controllers/application_controller.rb:19
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 2], ["LIMIT", 1]]
↳ app/controllers/reactions_controller.rb:4
Reaction Load (0.1ms) SELECT "reactions".* FROM "reactions" WHERE "reactions"."to_user_id" = ? AND "reactions"."from_user_id" = ? LIMIT ? [["to_user_id", 17], ["from_user_id", 2], ["LIMIT", 1]]
↳ app/controllers/reactions_controller.rb:4
(0.0ms) begin transaction
↳ app/controllers/reactions_controller.rb:5
Photo Load (0.1ms) SELECT "photos".* FROM "photos" WHERE "photos"."id" = ? LIMIT ? [["id", 17], ["LIMIT", 1]]
↳ app/controllers/reactions_controller.rb:5
Photo Load (0.1ms) SELECT "photos".* FROM "photos" WHERE "photos"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]]
↳ app/controllers/reactions_controller.rb:5
(0.0ms) commit transaction
↳ app/controllers/reactions_controller.rb:5
No template found for ReactionsController#create, rendering head :no_content
Completed 204 No Content in 306ms (ActiveRecord: 0.7ms)

回答2件
あなたの回答
tips
プレビュー