From 71aaa32d15fb5ac2690a54bd14364e154e4ee1cd Mon Sep 17 00:00:00 2001 From: Christophe Robillard Date: Wed, 28 Jan 2026 16:34:06 +0100 Subject: [PATCH] add user to moods --- app/models/mood.rb | 2 ++ app/models/user.rb | 1 + db/migrate/20260127150339_add_user_to_moods.rb | 5 +++++ db/schema.rb | 7 +++++-- 4 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20260127150339_add_user_to_moods.rb diff --git a/app/models/mood.rb b/app/models/mood.rb index 5a1ab13..2f1de4c 100644 --- a/app/models/mood.rb +++ b/app/models/mood.rb @@ -1,4 +1,6 @@ class Mood < ApplicationRecord + belongs_to :user + class << self def history_for_a_year history(Date.today - 1.year, Date.today) diff --git a/app/models/user.rb b/app/models/user.rb index b646207..e07b5f0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,6 +1,7 @@ class User < ApplicationRecord has_secure_password has_many :sessions, dependent: :destroy + has_many :moods normalizes :email_address, with: ->(e) { e.strip.downcase } diff --git a/db/migrate/20260127150339_add_user_to_moods.rb b/db/migrate/20260127150339_add_user_to_moods.rb new file mode 100644 index 0000000..620dca6 --- /dev/null +++ b/db/migrate/20260127150339_add_user_to_moods.rb @@ -0,0 +1,5 @@ +class AddUserToMoods < ActiveRecord::Migration[8.0] + def change + add_reference :moods, :user, foreign_key: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 8e352f9..4395fff 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,12 +10,14 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2026_01_27_134053) do +ActiveRecord::Schema[8.0].define(version: 2026_01_27_150339) do create_table "moods", force: :cascade do |t| t.string "mode" t.datetime "recorded_at" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "user_id" + t.index ["user_id"], name: "index_moods_on_user_id" end create_table "rfid_tags", force: :cascade do |t| @@ -25,7 +27,7 @@ ActiveRecord::Schema[8.0].define(version: 2026_01_27_134053) do t.datetime "updated_at", default: -> { "CURRENT_DATE" }, null: false t.string "chip_id" t.integer "user_id" - t.index ["identifier"], name: "index_rfid_tags_on_identifier", unique: true + t.index ["identifier"], name: "index_rfid_tags_on_identifier" t.index ["user_id"], name: "index_rfid_tags_on_user_id" end @@ -47,6 +49,7 @@ ActiveRecord::Schema[8.0].define(version: 2026_01_27_134053) do t.index ["email_address"], name: "index_users_on_email_address", unique: true end + add_foreign_key "moods", "users" add_foreign_key "rfid_tags", "users" add_foreign_key "sessions", "users" end