保存嵌套属性时,父级必须存在

时间:2018-11-22 05:28:22

标签: ruby-on-rails activerecord

我有两个模型slidecards

class Slide < ApplicationRecord
  has_many :cards,-> { order(:position) },dependent: :destroy
  accepts_nested_attributes_for :cards,reject_if: proc { |attr| attr['title'].blank?}
end

SlideController

  def create
    @slide = Slide.new(get_params)
    @slide.save
  end

当我尝试用卡片保存幻灯片时,出现错误

  

@messages = {:“ cards.slide” => [“必须存在”]},@details = {:“ cards.slide” => [{:error =>:blank}]}>

如果我从-> { order(:position) }中删除了has_many,那么一切正常。如果我正确,则在提交嵌套属性时会首先创建幻灯片,这是怎么回事?

0 个答案:

没有答案