Undefined method `image' in Shrine

792 Views Asked by At

I try to add Shrine gem to an existing project that uses Paperclip. I added a Shrine image to a new model (just for a check). So I created model Country with this db table:

  create_table "countries", force: :cascade do |t|
    t.string "name"
    t.text "image_data"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

and this model:

class Country < ApplicationRecord
  include ImageUploader::Attachment(:image)

  validates :name, length: { in: 2..180 }, presence: true
end

After that I tried to get image from country and received this (image_data works, though):

enter image description here

I thought that maybe it's because image is empty, but in another app with shrine everything works correctly

enter image description here

Does anyone know how to fix this?

2

There are 2 best solutions below

0
On BEST ANSWER

It seems that somehow shrine uploader was loaded before shrine.rb file so that I couldn't use any shrine methods. Similar issue can be found here: https://github.com/shrinerb/shrine/issues/155

Adding require_relative '../../config/initializers/shrine' to the uploader resolved the issue

0
On

I got the error in shrine version 3.2.1 and fixed it by adding Shrine.plugin :activerecord in shrine.rb. hope it can help.

https://shrinerb.com/docs/plugins/activerecord