如果category_attributes(:title)为空,我怎么能销毁这个对象?
def categories_attributes=(categories_attributes)
categories_attributes.values.each do |category_attribute|
category = Category.find_or_create_by(category_attribute)
categories << category
end
end
答案 0 :(得分:1)
试试这个:
def categories_attributes=(categories_attributes)
categories_attributes.values.each do |category_attribute|
category = Category.find_or_create_by(category_attribute)
if category.title?
categories << category
elsif category.persisted?
category.destroy
end
end
end