質問編集履歴

1

モデルの追加

2023/03/01 13:50

投稿

saki
saki

スコア11

test CHANGED
File without changes
test CHANGED
@@ -92,8 +92,39 @@
92
92
  <% end %>
93
93
  ```
94
94
  ```app/models/post.rb
95
+ class Post < ApplicationRecord
96
+ mount_uploader :post_image, PostImageUploader
97
+
98
+ belongs_to :user
99
+ has_many :comments, dependent: :destroy
100
+
101
+ validates :title, presence: true, length: { maximum: 255 }
102
+ validates :body, presence: true, length: { maximum: 65_535 }
103
+ validates :price, presence: true, numericality: {only_integer: true, greater_than_or_equal_to: 0}
104
+ validates :whiskey_brand, presence: true, length: { maximum: 255 }
105
+ validates :countries, presence: true
106
+ validates :whiskey_types, presence: true
107
+
108
+ has_many :countries, dependent: :destroy
109
+ accepts_nested_attributes_for :countries, allow_destroy: true
110
+
111
+ has_many :whiskey_types, dependent: :destroy
112
+ accepts_nested_attributes_for :whiskey_types, allow_destroy: true
95
- enum country: { scotland: 0, ireland: 1, america: 2, canada: 3, japan: 4, taiwan: 5, india: 6, others: 7 }, _prefix: true
113
+     enum country: { scotland: 0, ireland: 1, america: 2, canada: 3, japan: 4, taiwan: 5, india: 6, others: 7 }, _prefix: true
96
- enum whiskey_type: { malt: 0, grain: 1, belended: 2, bourbon: 3, rye: 4, corn: 5, others: 6 }, _prefix: true
114
+     enum whiskey_type: { malt: 0, grain: 1, belended: 2, bourbon: 3, rye: 4, corn: 5, others: 6 }, _prefix: true
115
+ end
116
+ ```
117
+ ```app/models/country.rb
118
+ class Country < ApplicationRecord
119
+ belongs_to :post, optional: true
120
+ validates :name, presence: true
121
+ end
122
+ ```
123
+ ```app/models/whiskey_type.rb
124
+ class WhiskeyType < ApplicationRecord
125
+ belongs_to :post, optional: true
126
+ validates :name, presence: true
127
+ end
97
128
  ```
98
129
  ```config/locals/enums.ja.yml
99
130
  ja: