0
比方說,我有這樣的簡單關聯:保存在同一時間,錯誤HANDELING
class Post < ActiveRecord::Base
has_many :comments # :autosave option is not declared
end
而這種代碼:
post = Post.new(title: 'ruby rocks')
post.comments.build(body: 'hello world')
post.save # => saves both post and comment
會發生什麼,如果post
是無效的,它仍然創造有何評論?
如果連接的comment
無效,會發生什麼情況?
我想那comment
或post
是無效的,它什麼也不保存。我做對了嗎?
我需要validates_associated
?謝謝
意思是我應該使用'validates_associated' –
是的,把validates_associated放在你的Post模型中,但不是在你的Comment模型中。 – mysmallidea