John C
John C

Reputation: 3

overriding ActiveRecord.save to read in property before save

I have this in my object's save

class UploadFile < ActiveRecord::Base
def save
    dir = 'public/data'
    path = File.join(dir, 'nfile')

    from = contents.path
    contents = `cat #{from}`
    super
end

end

contents stores a file object from a multi-part form submit.

This is very quick and dirty (yes I know cat #{from} is probably not a good idea). Why is it that after super is called contents is # and not the contents of the file?

Thanks.

Upvotes: 0

Views: 1851

Answers (1)

macarthy
macarthy

Reputation: 3069

Use the before_save callback http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

Upvotes: 1

Related Questions