###前提・実現したいこと
ruby(ruby on rails)でユーザーがひとつのフォームを入力したとき、
複数のモデルにレコードを作成したいと考えています。
具体的に言うと、ユーザーが所有するアイテムの情報を入力したときに、
アイテムストック(item_stock)モデルと、
入出庫(loading_and_unloading)モデルに、
ストロングパラメータを使ってまとめて関連付け有の状態で新規作成したいのです。
例えば、アイテムストックモデルには、user_idが記録され、
入出庫モデルにはアイテムストック_idが記録されて、
誰がどのアイテムを持ち、どのアイテムがどれだけ増減したかという具合に。
しかし、
NoMethodError in ItemStocksController#create
undefined method `loading_and_unloading_type_id=' for #LoadingAndUnloading::ActiveRecord_Associations_CollectionProxy:0x007fb97cebe320
というエラーが発生してしまいます。
どうすればこのエラーを解消して2つの親子的関係にあるモデルにレコードを作成することができるのか教えて頂ければ幸いです。
###現状
modelの関係は図にすると以下のようになります。
ユーザーはアイテムストックを多数所有し、アイテムストックは入出庫を多数所有する関係にあります。
class User < ApplicationRecord #一人のユーザーは多数のitem_stocksを所有している has_many :item_stocks # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable end
class ItemStock < ApplicationRecord #複数のItemStockモデルは一人のユーザーモデルに従属している belongs_to :user #複数のItemStockモデルは一つの所有物状態モデルに従属している belongs_to :property_state #enum(フラグやステータス管理)の定義、使用状況に関する定義 enum usage_state: { unsettled_items: 0, value_items: 1, unavailable: 2 } end
class LoadingAndUnloading < ApplicationRecord #複数のLoadingAndUnloading(入出庫)モデルは、一つのItemStockモデルに従属している belongs_to :item_stock #複数のLoadingAndUnloading(入出庫)モデルは、一つのLoadingAndUnloadingType(入出庫形式)モデルに従属している belongs_to :loading_and_unloading_type end
コントローラファイルの記述です。おそらく今回の問題の原因となる箇所だと思われます。
item_stocks_controller.rb
class ItemStocksController < ApplicationController def new @unsettled_sell_item = ItemStock.new end def create begin ActiveRecord::Base.transaction{ #アイテムストックモデルにレコード(親)を作成 #unsettled_sell_item_paramsには、ユーザーが入力した値が格納されている @unsettled_sell_item = ItemStock.new(unsettled_sell_item_params) #その他アイテムストックのレコードに自動的に記録したい内容 @unsettled_sell_item.user_id = current_user.id @unsettled_sell_item.property_state_id = 1 @unsettled_sell_item.usage_state = 0 #フラグ記録 @unsettled_sell_item.purchase_price = 0 @unsettled_sell_item.save!#アイテムストックモデルに記録 #入出庫モデルにレコード(子)を作成(問題発生箇所) @unsettled_sell_item.loading_and_unloadings.build @unsettled_sell_item.loading_and_unloadings.loading_and_unloading_type_id = 1 @unsettled_sell_item = [:item_stock][:exhibition_quantity] @unsettled_sell_item = [:item_stock][:sale_price] @unsettled_sell_item.save! #入出庫モデルに保存 } #完全成功なら、root_path(ホーム画面へ) redirect_to root_path end end private def unsettled_sell_item_params #ネスト(子レコードも作成)を考慮したパラメータ設定、子モデルloading_and_unloadingに渡すパラメータは、quantity, :priceだけ許可する。 params.require(:item_stock).permit(:item_name, :sale_price, :description, :image_url, :exhibition_quantity, :expiration_date, loading_and_unloading: [:quantity, :price]) end end
ビューファイルです。
views/item_stocks/new.html.erb
<h1>アイテム新規登録</h1> <%= form_for @unsettled_sell_item do |f| %> <% end %> <div class="field"> <%= f.label :アイテム名 %> <%= f.text_field :item_name %> </div> <div class="field"> <%= f.label :出品価格 %> <%= f.number_field :sale_price,min:1,max:999999999 %> </div> <div class="field"> <%= f.label :説明文 %> <%= f.text_field :description %> </div> <div class="field"> <%= f.label :出品数 %> <%= f.number_field :exhibition_quantity, min:1,max:999999 %> </div> <div class="field"> <%= f.label :使用期限 %> <%= f.datetime_select :expiration_date, start_year: now.year, end_year: now.year + 100 %> </div> <%= f.submit"保存する" %> <div> <%= @unsettled_sell_item.errors.to_yaml %> </div> <% end %>
###補足情報(言語/FW/ツール等のバージョンなど)
Rails 5.1.3
ruby 2.4.1
devise (4.3.0)
あなたの回答
tips
プレビュー