vendredi 25 novembre 2016

Save at same time, error handeling

Let's say I have this simple association:

class Post < ActiveRecord::Base
  has_many :comments # :autosave option is not declared
end

And this code:

post = Post.new(title: 'ruby rocks')
post.comments.build(body: 'hello world')
post.save # => saves both post and comment

What happens if post is invalid, does it still create the comment?

What happens if the attached comment is invalid, does it still create the post?

I would like that when comment or post is invalid, it saves nothing. Am I doing the right thing?

Do I need validates_associated ?Thanks

Aucun commentaire:

Enregistrer un commentaire