Skip to content

Commit ad30dff

Browse files
committed
feat: comment model added
1 parent 475d51d commit ad30dff

File tree

6 files changed

+43
-1
lines changed

6 files changed

+43
-1
lines changed

app/models/comment.rb

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Comment < ApplicationRecord
2+
validates :body, presence: true
3+
4+
belongs_to :product
5+
belongs_to :user
6+
end

app/models/product.rb

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Product < ApplicationRecord
1010
}
1111

1212
belongs_to :user
13+
has_many :comments, dependent: :destroy
1314

1415
def owned_by?(owner)
1516
user == owner
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class CreateComments < ActiveRecord::Migration[6.0]
2+
def change
3+
create_table :comments do |t|
4+
t.text :body
5+
t.references :product, null: false, foreign_key: true
6+
t.references :user, null: false, foreign_key: true
7+
8+
t.timestamps
9+
end
10+
end
11+
end

db/schema.rb

+13-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,17 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2019_11_04_101016) do
13+
ActiveRecord::Schema.define(version: 2019_11_05_032008) do
14+
15+
create_table "comments", force: :cascade do |t|
16+
t.text "body"
17+
t.integer "product_id", null: false
18+
t.integer "user_id", null: false
19+
t.datetime "created_at", precision: 6, null: false
20+
t.datetime "updated_at", precision: 6, null: false
21+
t.index ["product_id"], name: "index_comments_on_product_id"
22+
t.index ["user_id"], name: "index_comments_on_user_id"
23+
end
1424

1525
create_table "products", force: :cascade do |t|
1626
t.string "name"
@@ -34,5 +44,7 @@
3444
t.index ["email"], name: "index_users_on_email", unique: true
3545
end
3646

47+
add_foreign_key "comments", "products"
48+
add_foreign_key "comments", "users"
3749
add_foreign_key "products", "users"
3850
end

spec/factories/comments.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FactoryBot.define do
2+
factory :comment do
3+
body { "MyText" }
4+
product { "" }
5+
user { nil }
6+
end
7+
end

spec/models/comment_spec.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe Comment, :type => :model do
4+
pending "add some examples to (or delete) #{__FILE__}"
5+
end

0 commit comments

Comments
 (0)