File tree 6 files changed +43
-1
lines changed
6 files changed +43
-1
lines changed Original file line number Diff line number Diff line change
1
+ class Comment < ApplicationRecord
2
+ validates :body , presence : true
3
+
4
+ belongs_to :product
5
+ belongs_to :user
6
+ end
Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ class Product < ApplicationRecord
10
10
}
11
11
12
12
belongs_to :user
13
+ has_many :comments , dependent : :destroy
13
14
14
15
def owned_by? ( owner )
15
16
user == owner
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change 10
10
#
11
11
# It's strongly recommended that you check this file into your version control system.
12
12
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
14
24
15
25
create_table "products" , force : :cascade do |t |
16
26
t . string "name"
34
44
t . index [ "email" ] , name : "index_users_on_email" , unique : true
35
45
end
36
46
47
+ add_foreign_key "comments" , "products"
48
+ add_foreign_key "comments" , "users"
37
49
add_foreign_key "products" , "users"
38
50
end
Original file line number Diff line number Diff line change
1
+ FactoryBot . define do
2
+ factory :comment do
3
+ body { "MyText" }
4
+ product { "" }
5
+ user { nil }
6
+ end
7
+ end
Original file line number Diff line number Diff line change
1
+ require 'rails_helper'
2
+
3
+ RSpec . describe Comment , :type => :model do
4
+ pending "add some examples to (or delete) #{ __FILE__ } "
5
+ end
You can’t perform that action at this time.
0 commit comments