Tasos Anesiadis
Tasos Anesiadis

Reputation: 1150

Rails, uploading multiple images with carrierwave, but json not supported by my database

I am trying to use Carrierwave with multiple image uploads. After following the carrierwave guide on github I do:

rails g migration add_images_to_areas images:json
rake db:migrate

But seeing on my schema, my areas table does not show up, instead I get:

# Could not dump table "areas" because of following StandardError
#   Unknown type 'json' for column 'images'

What should I do now? Should I use another type instead of json, but what?

Sorry if this is an amateur question.

Upvotes: 1

Views: 1315

Answers (2)

Tasos Anesiadis
Tasos Anesiadis

Reputation: 1150

I am not sure as to what was going wrong here, The github tutorial of Carrierwave was suggesting :json, but you don't have to do so.

Everything went fine after I just followed the guide for multiple image upload with Carrierwave here: Rails 4 multiple image or file upload using carrierwave

Upvotes: 1

Ruben Barbosa
Ruben Barbosa

Reputation: 151

Databases by default don't support arrays, hashes and so on.

In order to do it, you can serialize it adding this code to your model:

class Name_class < ActiveRecord::Base
  serialize :column_name, JSON
end

And change the migration field :

add_column :user_preferences, :text

This will insert the info as Text into the database and, when you retrieve it , it will be JSON.

More info about serialization here RailsGuides#Serialize

Upvotes: 2

Related Questions