RoR: accessing page generated in after_save callback
I'm having a problem understanding the after_save rails callback. There is a snippet from my model:
after_save :log_creation
private
def log_creation
logger.info "RECEIVED => #{ self.name } - #{ self.value } - #{ self.id }"
path_to_url = url_for({ :host => "localhost:8080", :action=>"show", :controller=>"testing", :format =>"pdf", :id => self })
logger.info 开发者_JS百科path_to_url
file = open(path_to_url, "r").read
logger.info file
end
In the development log I get the expected information for the first two logger.info. The third one I don't get it, because I receive a 404 exception (ActiveRecord::RecordNotFound).
So, what I'm doing wrong?
The PDF generation works OK (using PDFkit), if I comment the after_save line and try to access the URL it's OK, but when I try to access it on after_save it doesn't work as I excepected.
All of this is for sending an email (with a mailer) with a PDF attached on it. It would download the PDF (through the controlled) and then send it.
I think the problem has to do with transactions. The new model has been saved, but the transaction hasn't ended yet. Try after_commit
instead of after_save
. See the documentation on callbacks for details about all the different callbacks.
精彩评论