I'm working with a controller that follows the resource_controller principle explained here.
As you can see, I don't have full control on the (new, create etc...) all of them preload data, which means if I want to create a new entity let's say @user
the following code has already been called @user = User.new
.
The only way I can include some validation is through callbacks, but once the callback decides to cancel the new method, I always end 开发者_开发百科up with a Hash frozen error.
In other words, I can't say
@user = User.new
@user.destroy
The only hack I was able to come up with was
@user = User.new
@user.save
@user.destroy
Does anyone know a better way, which doesn't involve saving things to the database? And btw what is the difference between active record and active model?
How about checking if it's been saved before destroying it?
@user.destroy unless (@user.new_record?)
精彩评论