簡體   English   中英

帶reject_if的accepts_nested_attributes_for的定制屬性版本

[英]custom attributes version of accepts_nested_attributes_for with reject_if

我試圖在下面創建一個自定義版本

  accepts_nested_attributes_for :categories, :reject_if => proc { |hash| hash['title'].blank? }

到目前為止,我得到的是

  def categories_attributes=(categories_attributes)
    categories_attributes.values.each do |category_attribute|
      category = Category.find_or_create_by(category_attribute)
      categories << category
    end
  end

但是我試圖在其中添加reject_if部分,我似乎無法弄清楚。 我如何將該部分復制到下面的自定義屬性中?

謝謝!

這樣做

accepts_nested_attributes_for :categories, reject_if: :title_blank

def title_blank(attributed)
  data = false
  data = true if attributed.title.blank?
  return data
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM