Rails: rollback all transactions if one fails -


so have configurations

class post < activerecord::base has_many :photo_albums  class photoalbum < activerecord::base has_many :photos 

and want following

@post = post.new(post_params) @photo_album = @post.photo_albums.create(name: 'album name') @photo_urls = params[:photo_urls] @photo_urls.each |pu|  @photo_album.photos.create(url: pu) end @post.save 

now want that, if @post.save fails transactions of photo_albums, photos should rollback.

you can add if filter if @post.save , write code block create photo_album , photos if @post saved

@post = post.new(post_params) if @post.save   @photo_album = @post.photo_albums.create(name: 'album name')   @photo_urls = params[:photo_urls]   @photo_urls.each |pu|    @photo_album.photos.create(url: pu)   end  else   # handle @post.save fail end 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -