diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index 6c6ff00..11f2df7 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -22,6 +22,7 @@ def edit # POST /courses or /courses.json def create @course = Course.new(course_params) + @course.user = current_user respond_to do |format| if @course.save diff --git a/app/models/course.rb b/app/models/course.rb index 83c0340..8453506 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -2,6 +2,7 @@ class Course < ApplicationRecord validates :title, presence: true validates :description, presence: true, length: { minimum: 5 } + belongs_to :user def to_s title end diff --git a/app/models/user.rb b/app/models/user.rb index 4756799..eb7c698 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,4 +3,10 @@ class User < ApplicationRecord # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable + + def to_s + email + end + + has_many :courses end diff --git a/app/views/courses/index.html.haml b/app/views/courses/index.html.haml index 2b70590..47f3002 100644 --- a/app/views/courses/index.html.haml +++ b/app/views/courses/index.html.haml @@ -7,6 +7,7 @@ %tr %th Title %th Description + %th User %th %th %th @@ -16,6 +17,7 @@ %tr %td= course.title %td= course.description + %td= course.user %td= link_to 'Show', course %td= link_to 'Edit', edit_course_path(course) %td= link_to 'Destroy', course, method: :delete, data: { confirm: 'Are you sure?' } diff --git a/db/migrate/20241012032156_add_user_to_courses.rb b/db/migrate/20241012032156_add_user_to_courses.rb new file mode 100644 index 0000000..a19adb0 --- /dev/null +++ b/db/migrate/20241012032156_add_user_to_courses.rb @@ -0,0 +1,5 @@ +class AddUserToCourses < ActiveRecord::Migration[7.2] + def change + add_reference :courses, :user, null: false, foreign_key: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 8981af2..2c2f2ff 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2024_10_08_045010) do +ActiveRecord::Schema[7.2].define(version: 2024_10_12_032156) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -57,6 +57,8 @@ t.text "description" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.bigint "user_id", null: false + t.index ["user_id"], name: "index_courses_on_user_id" end create_table "users", force: :cascade do |t| @@ -73,4 +75,5 @@ add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id" + add_foreign_key "courses", "users" end diff --git a/db/seeds.rb b/db/seeds.rb index 86f9132..baad038 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,6 +1,9 @@ +User.create!(email: 'admin@admin.com', password: 'Password1234', password_confirmation: 'Password1234') + 30.times do Course.create!([ { title: Faker::Educator.course_name, - description: Faker::TvShows::GameOfThrones.quote + description: Faker::TvShows::GameOfThrones.quote, + user_id: User.first.id } ]) end