Search code examples
ruby-on-railsrubyruby-on-rails-4carrierwave

Sending information from image uploader to the model carrierwave


So currently i have my table with a width column and a height column,

def store_dimensions
if file && model
  model.width, model.height = ::MiniMagick::Image.open(file.file[:dimensions])
 end
end

I use this method to set the height and width so when in validation methods in the model i can validate that the dimensions are valid.

I wondering if there was a better way to send params over to the model instead of persisting them to the database because after validation they arent used again.

Ive tried to add

attr_accessor :height, width

to the uploader and the model then setting the variables in store dimensions method but cant retrieve them on the model end.

If anyone has had any experience doing this could you shed some light thanks


Solution

  • To use some fields only for validations, one can define attr_accessor on the model, but need to keep in mind that these attributes would only be available when assigned explicitly(since not backed by DB columns).

    For current scenario, you can keep your current code and define attr_accessor :height, :width on the model and use them in validations.