1

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.

Tasos Anesiadis
  • 1,134
  • 1
  • 12
  • 37

2 Answers2

2

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

Ruben Barbosa
  • 151
  • 1
  • 12
  • 2
    Databases support arrays or json, with some extensions. – Roman Kiselenko May 30 '16 at 09:23
  • 1
    I didn't know that . I always use serialize when i need a json or array field in the database. Thank you for the info though, – Ruben Barbosa May 30 '16 at 09:33
  • 1
    @RubenBarbosa thank for the tip, I will try it and get right back at you. – Tasos Anesiadis May 30 '16 at 10:08
  • @Зелёный Is there an easy way for mysql to support json? I know Postgres can support it, should I change to that? – Tasos Anesiadis May 30 '16 at 10:08
  • 1
    Hollywar detected, choise by yourself. https://dev.mysql.com/doc/refman/5.7/en/json.html – Roman Kiselenko May 30 '16 at 10:11
  • @RubenBarbosa I tried what you said but it does not work. The :images attribute is always nil. In my controller I have `params.require(:area).permit(:id, {images: []})` How do you handle that in your controller? – Tasos Anesiadis May 30 '16 at 14:02
  • 1
    Can you put the code of your controller?, the action when you save it in the database. I think the problem is there. – Ruben Barbosa May 30 '16 at 16:43
  • 1
    About the permit stuff, that should be okay. It's the right syntax and it should be working. It could be the `create` action ? – Ruben Barbosa May 30 '16 at 17:56
  • 1
    @RubenBarbosa I actually followed a the answer on this post http://stackoverflow.com/questions/21411988/rails-4-multiple-image-or-file-upload-using-carrierwave and made it work. Also it seems like it has to be `add_column :user_preferences, :string` – Tasos Anesiadis Jun 01 '16 at 11:58
  • 1
    Text and String only differ in the capacity of the field , but i'm glad you solve your problem. Answer your own question and mark it as Answered, it will be of help for others. – Ruben Barbosa Jun 01 '16 at 12:01
  • @RubenBarbosa I did, thanks for trying to help anyway! – Tasos Anesiadis Jun 01 '16 at 12:50
1

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

Community
  • 1
  • 1
Tasos Anesiadis
  • 1,134
  • 1
  • 12
  • 37