2つのモデルに同時に保存するにはどうしたらよいでしょうか?
この場合の保存というのは新規でテーブルを生成するという意味です。
出品アイテムにコメントがついた場合にnotificationにitem_idを書き込みたいです。
lang
1#Comment model 2 def create 3 comment = current_user.comments.build do |t| 4 t.item_id = params[:item_id] 5 t.body = params[:comment][:body] 6 end 7 8 9 10 Notification.create(user_id: current_user.id, item_id: params[:item_id], status: "1") 11 12 13 14 if comment.save 15 flash[:notice] = 'コメントしました。' 16 head 201 17 else 18 render json: { messages: comment.errors.full_messages }, status: 422 19 end 20 end 21 22 23
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。