質問編集履歴

1

モデル情報の追加

2021/08/27 04:30

投稿

whakapapa-HTML
whakapapa-HTML

スコア0

test CHANGED
File without changes
test CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
 
8
8
 
9
-
9
+ **Recipeファクトリー**
10
10
 
11
11
 
12
12
 
@@ -50,11 +50,91 @@
50
50
 
51
51
 
52
52
 
53
+ **Recipe.rb**
54
+
55
+ ```
56
+
57
+ class Recipe < ApplicationRecord
58
+
59
+ include ActiveModel::Dirty
60
+
61
+
62
+
63
+ has_many :reviews, dependent: :destroy
64
+
65
+ has_many :bookmarks, dependent: :destroy
66
+
67
+ has_many :ingredients, dependent: :destroy
68
+
69
+ has_many :procedures, dependent: :destroy
70
+
71
+ belongs_to :user, optional: true
72
+
73
+ belongs_to :genre
74
+
75
+
76
+
77
+ attr_accessor :recipe_image_cache
78
+
79
+
80
+
81
+ mount_uploader :recipe_image, RecipeImageUploader
82
+
83
+
84
+
85
+ accepts_nested_attributes_for :ingredients, :procedures, allow_destroy: true
86
+
87
+
88
+
89
+ with_options presence: true do
90
+
91
+ validates :serving
92
+
93
+ validates :name
94
+
95
+ validates :recipe_image
96
+
97
+ validates :introduction
98
+
99
+ validates :ingredients
100
+
101
+ end
102
+
103
+ ```
104
+
105
+
106
+
107
+ **Ingredient.rb**
108
+
109
+ ```
110
+
111
+ class Ingredient < ApplicationRecord
112
+
113
+ belongs_to :recipe
114
+
115
+
116
+
117
+ with_options presence: true do
118
+
119
+ validates :name
120
+
121
+ validates :amount
122
+
123
+ end
124
+
125
+ end
126
+
127
+
128
+
129
+ ```
130
+
131
+
132
+
53
133
  ### 該当のコード
54
134
 
55
135
 
56
136
 
57
- この方法だとうまくいきません。
137
+ **recipe_spec.rb**
58
138
 
59
139
  ```
60
140