Skip to content

Commit

Permalink
Add relations between users and entries
Browse files Browse the repository at this point in the history
  • Loading branch information
marclerodrigues committed Dec 8, 2019
1 parent 78c65d2 commit de55851
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/models/entry.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
class Entry < ApplicationRecord
belongs_to :user
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions db/migrate/20191208030319_add_users_to_entries.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddUsersToEntries < ActiveRecord::Migration[6.0]
def change
add_reference :entries, :user, foreign_key: true, index: true
end
end
5 changes: 4 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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|
Expand All @@ -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
6 changes: 4 additions & 2 deletions spec/models/entry_spec.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 4 additions & 2 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit de55851

Please sign in to comment.