Can't create file field with ActiveStorage in Rails
Asked Answered
S

2

7

I'm trying to use Active Storage in Rails 5.2. I found that I should create field with type file in migration, but I have an error:

$ rdm
Running via Spring preloader in process 40193
== 20171217191942 CreateDishes: migrating 
=====================================
-- create_table(:dishes)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

undefined method `file' for #<ActiveRecord::ConnectionAdapters::PostgreSQL::TableDefinition:0x00007fd56e297750>
/Users/alder/Projects/_apps/service_exchange/rails-backend/db/migrate/20171217191942_create_dishes.rb:6:in `block in change'
/Users/alder/Projects/_apps/service_exchange/rails-backend/db/migrate/20171217191942_create_dishes.rb:3:in `change'
-e:1:in `<main>'

Migration:

class CreateDishes < ActiveRecord::Migration[5.2]
  def change
    create_table :dishes do |t|
      t.string :name, index: true
      t.string :description
      t.file :image

      t.timestamps
    end
  end
end

I was trying to create string field, but it doesn't work.

Couldn't find anything about that in official docs

I have migration for active storage and I passed ok

Seritaserjeant answered 18/12, 2017 at 4:52 Comment(4)
i'm pretty sure there's no such data type 'file' in rails migration, try using f.stringTithable
Check this blog: rubyinrails.com/2017/07/21/…Stipe
So, yeah, I used exactly that link. If you make string field it shows type mismatch later.Seritaserjeant
what class is the migration inheriting from? Ensure it's the latest migration version to pick up on new column types within ActiveRecord, ActiveRecord::Migration[5.2]Broeker
M
12

Instead of a dedicated field that you need to create in your own migration (t.file :image) active storage uses two tables that you setup with rails active_storage:install. When you setup your storage.yml you should be able to use

has_one_attached :image

within the Dishesmodel without creating a imagecolumn.

Miniskirt answered 24/12, 2017 at 0:37 Comment(2)
I thought that probably I shouldn't create any field for image, I'll check it later, but I guess It's correct answer.Seritaserjeant
I'm having issues with using :avatar as the name of the data field, could this be a problem?Agustinaah
A
4

You can check this question (ActiveRecord field type) because did not have any type like file if you need to upload a file you can create with type string like t.string

Antipope answered 22/12, 2017 at 9:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.