简体   繁体   中英

Callback for creation of nested model instance

class Category < ActiveRecord::Base
  has_many    :posts
  accepts_nested_attributes_for :posts
end

Is it possible in the Category model to understand if there's a Post instance being created along with the saving of a Category instance?

You can query the posts collection from the category instance to see if any new records exist. If so they'll be saved when category is.

category.posts.any?(&:new_record?)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM