diff --git a/app/models/entry.rb b/app/models/entry.rb index fae23de..1460619 100644 --- a/app/models/entry.rb +++ b/app/models/entry.rb @@ -1,2 +1,3 @@ class Entry < ApplicationRecord + belongs_to :user end diff --git a/app/models/user.rb b/app/models/user.rb index 4756799..3677bf8 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,4 +3,6 @@ class User < ApplicationRecord # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable + + has_many :entries, dependent: :destroy end diff --git a/db/migrate/20191208030319_add_users_to_entries.rb b/db/migrate/20191208030319_add_users_to_entries.rb new file mode 100644 index 0000000..6539237 --- /dev/null +++ b/db/migrate/20191208030319_add_users_to_entries.rb @@ -0,0 +1,5 @@ +class AddUsersToEntries < ActiveRecord::Migration[6.0] + def change + add_reference :entries, :user, foreign_key: true, index: true + end +end diff --git a/db/schema.rb b/db/schema.rb index a875fe4..4465e9a 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.define(version: 2019_10_20_164632) do +ActiveRecord::Schema.define(version: 2019_12_08_030319) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -22,6 +22,8 @@ t.datetime "updated_at", precision: 6, null: false t.date "day" t.time "hour" + t.bigint "user_id" + t.index ["user_id"], name: "index_entries_on_user_id" end create_table "users", force: :cascade do |t| @@ -37,4 +39,5 @@ t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end + add_foreign_key "entries", "users" end diff --git a/spec/models/entry_spec.rb b/spec/models/entry_spec.rb index 3832688..ea7d42f 100644 --- a/spec/models/entry_spec.rb +++ b/spec/models/entry_spec.rb @@ -1,5 +1,7 @@ -require 'rails_helper' +require "rails_helper" RSpec.describe Entry, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + describe "relations" do + it { is_expected.to belong_to(:user) } + end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 47a31bb..560c7ee 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1,5 +1,7 @@ -require 'rails_helper' +require "rails_helper" RSpec.describe User, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + describe "relations" do + it { is_expected.to have_many(:entries).dependent(:destroy) } + end end