質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

1634閲覧

unknown attribute 'recruitment_id' for Order.というエラーを解決したいです

yuki0302

総合スコア3

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2021/11/28 09:06

前提・実現したいこと

フォームオブジェクトを用いた購入機能の実装。
コントローラーでエラーが出ています。

発生している問題・エラーメッセージ

エラーメッセージ ActiveModel::UnknownAttributeError in OrdersController#create unknown attribute 'recruitment_id' for Order.
ターミナル Recruitment Load (0.5ms) SELECT `recruitments`.* FROM `recruitments` WHERE `recruitments`.`id` = 2 LIMIT 1 ↳ app/controllers/orders_controller.rb:23:in `recruitment_params' Unpermitted parameters: :number, :exp_month, :exp_year, :cvc User Load (0.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1 ↳ app/controllers/orders_controller.rb:27:in `order_params' Completed 500 Internal Server Error in 53ms (ActiveRecord: 12.8ms | Allocations: 23920) ActiveModel::UnknownAttributeError (unknown attribute 'recruitment_id' for Order.): app/controllers/orders_controller.rb:10:in `create'

該当のソースコード

コントローラー

class OrdersController < ApplicationController before_action :authenticate_user!, only: [:index ] before_action :recruitment_params, only: [:index, :create ] def index @order = Order.new end def create @order = Order.new(order_params) if @order.valid? pay_recruitment @order.save redirect_to root_path(@order) else render :index end end private def recruitment_params @recruitment = Recruitment.find(params[:recruitment_id]) end def order_params params.require(:order).permit( :telephone_number).merge(user_id: current_user.id, recruitment_id: params[:recruitment_id], token: params[:token]) end def pay_recruitment Payjp.api_key = ENV["PAYJP_SECRET_KEY"] # PAY.JPテスト秘密鍵 Payjp::Charge.create( amount: @recruitment.order, # 商品の値段 card: order_params[:token], # カードトークン currency: 'jpy' # 通貨の種類(日本円) ) end end

モデル

class Recruitment < ApplicationRecord has_one :record belongs_to :user has_one_attached :image extend ActiveHash::Associations::ActiveRecordExtensions belongs_to :category validates :heading, :content, :price, :image, :category_id, presence: true validates :category_id, numericality: { other_than: 1, message: "を入力してください"} validates :price, numericality: {only_integer: true, greater_than_or_equal_to: 500, less_than_or_equal_to: 999999, message: "を¥500〜999,999に訂正してください"} end
class Record < ApplicationRecord belongs_to :user belongs_to :recruitment has_one :order end
class Order < ApplicationRecord include ActiveModel::Model attr_accessor :telephone_number, :user_id, :item_id, :token with_options presence: true do validates :token validates :telephone_number, format:{with: /\A\d{10,11}\z/ } validates :user_id validates :recruitment_id end def save record = Record.create(recruitment_id: recruitment_id, user_id: user_id) Address.create(telephone_number: telephone_number, record_id: record.id) end end

マイグレーション

orders class CreateOrders < ActiveRecord::Migration[6.0] def change create_table :orders do |t| t.string :telephone_number, null: false t.references :record, null: false, foreign_key: true t.timestamps end end end
class CreateRecords < ActiveRecord::Migration[6.0] def change create_table :records do |t| t.references :user, null: false, foreign_key: true t.references :recruitment, null: false, foreign_key: true t.timestamps end end end

試したこと

マイグレーションの確認。
データベースにはorderにはrecruitment_idが入っています。

補足情報(FW/ツールのバージョンなど)

railsのバージョン
Rails 6.0.4.1

gemのバージョン
3.0.3

追記・修正があればよろしくお願いいたします

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

ApplicationRecord を継承しているモデルが include ActiveModel::Model としているからだと思います。
別のクラスを作り include ActiveModel::Model する必要があります。

ruby

1class Neko < ApplicationRecord 2end 3 4Neko.new(name: "tama") # => #<Neko...>

ruby

1class Neko < ApplicationRecord 2 include ActiveModel::Model 3end 4 5Neko.new(name: "tama") # => ActiveModel::UnknownAttributeError: unknown attribute 'name' for Neko.

投稿2021/11/29 03:52

neko_daisuki

総合スコア2090

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yuki0302

2021/11/29 10:45

ご回答ありがとうございます!! 別のクラスを作らず、ApplicationRecordを消したらうまくいきました!! 半日以上このエラーに費やしたのでとても助かりました!!ありがとうございました!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問