Maxence
Maxence

Reputation: 2339

Executing a function only once in model

I am trying to retrieve an image dimension from a temp file. Which is successful through the following:

after_post_process :save_ratio_image

  def save_ratio_image
    geo = Paperclip::Geometry.from_file(image.queued_for_write[:original])
    self.ratiolongdivlarg = geo.width / geo.height
  end

Though I need to repeatdly save this model over time because on top of the attachment I have some other fields and one more Paperclip attachment too.

The above code then throw an error Cannot find the geometry of a file with a blank name I guess this is because the temp file no longer exists the other times I save this model..

Is there a way to call this function only once (ie the first time a record from this model is saved) ?

Upvotes: 0

Views: 685

Answers (1)

sixty4bit
sixty4bit

Reputation: 7956

Try the after_create callback instead of what you're currently using. It will only call the function when the record is saved for the first time.

http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

Upvotes: 1

Related Questions