My ‘create’ function in my ‘Message’ controller is something like this:
def create
@message = Message.new(params[:message])
@message2 = Message.new(params[:message])
@message.sender_deleted = false
@message2.sender_deleted = true
if @message2.save
...
else
logger.debug("SAVE DIDN'T WORK")
For whatever reason, message2 cannot be saved, but @message can. I believe this is because you need to save only a variable named @message, but I can’t figure out how to get around this. I need to, on this save, save multiple things to the database – is there some other way to do this or am I doing this completely wrong?
Thanks for your help
There’s no reason you can’t save more than once in an action, though why you’d want to do such a thing is debatable. You’ll want to put the saves in a transaction so you only save when both records are valid.
save!will raise an exception when the save fails.