teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

eventとevent_tagのmodel追記しました

2020/11/27 09:19

投稿

souda-takeru
souda-takeru

スコア4

title CHANGED
File without changes
body CHANGED
@@ -111,5 +111,48 @@
111
111
  params.require(:events_tag).permit(:name, :explanation, :facility_id, :scale_id, :category_id, :volunteer, :tagname, images: []).merge(user_id: current_user.id)
112
112
  end
113
113
  end
114
+ ```_event_model
115
+ ```ruby
116
+ class Event < ApplicationRecord
117
+ extend ActiveHash::Associations::ActiveRecordExtensions
118
+ belongs_to :user
119
+ has_many_attached :images
120
+ belongs_to :facility
121
+ belongs_to :scale
122
+ belongs_to :category
123
+ has_many :event_tag_relations, dependent: :destroy
124
+ has_many :tags, through: :event_tag_relations, dependent: :destroy
125
+ end
126
+ ```event_tag.model
127
+ ```ruby
128
+ class EventsTag
129
+
130
+ include ActiveModel::Model
131
+ attr_accessor :name, :explanation, :facility_id, :scale_id, :category_id, :volunteer, :tagname, :user_id
132
+
133
+
134
+ with_options presence: true do
135
+ validates :name
136
+ validates :explanation
137
+ validates :facility_id
138
+ validates :scale_id
139
+ validates :category_id
140
+ end
141
+ with_options numericality: { other_than: 1 } do
142
+ validates :facility_id
143
+ validates :scale_id
144
+ validates :category_id
145
+ end
146
+
147
+
148
+ def save
149
+ event = Event.create(name: name, explanation: explanation, facility_id: facility_id, scale_id: scale_id, category_id: category_id, volunteer: volunteer,user_id: user_id)
150
+ tag = Tag.where(tagname: tagname).first_or_initialize
151
+ tag.save
152
+
153
+ EventTagRelation.create(event_id: event.id, tag_id: tag.id)
154
+ end
155
+
156
+ end
114
- ```_
157
+ ```
115
158
  ```