Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Carrierwave hits update with unchanged value #2468

Closed
airled opened this issue Apr 11, 2020 · 0 comments
Closed

Carrierwave hits update with unchanged value #2468

airled opened this issue Apr 11, 2020 · 0 comments

Comments

@airled
Copy link

airled commented Apr 11, 2020

Hi there.
Chunk of my migration:

  ...
  t.json :gallery, null: false, default: {}

Mounting:

  mount_uploaders :gallery, AmusementGalleryUploader

Uploader:

class AmusementGalleryUploader < CarrierWave::Uploader::Base
  include CarrierWave::MiniMagick

  storage :file

  def store_dir
    "uploads/amusement/#{mounted_as}/#{model.id}"
  end

  def extension_whitelist
    %w[jpg jpeg gif png svg]
  end

  def content_type_whitelist
    [/image\//]
  end
end

The issue is if I try to update my record with form and image has same name (I do not even change anything else in my record), carrierwave with rails hit update on database even if array of images wasn't changed. Moreover, I use paper trail in my project so it creates unnecessary db record.
I inspect rails behaviour with before_save callback and byebug:

(byebug) self.changed?
true
(byebug) self.changes
{"gallery"=>[["screen-2020-04-11-12-42-10.jpg"], ["screen-2020-04-11-12-42-10.jpg"]]}

Logs of request:

Started PUT "/admin/amusement/3/edit" for ::1 at 2020-04-11 20:03:18 +0600
Processing by RailsAdmin::MainController#edit as HTML
  Parameters: **{"gallery"=>["screen-2020-04-11-12-42-10.jpg"]}**
  ...
  Amusement Update (1.1ms)  UPDATE "amusements" SET "gallery" = $1, "updated_at" = $2 WHERE "amusements"."id" = $3  [["gallery", "[\"screen-2020-04-11-12-42-10.jpg\"]"], ["updated_at", "2020-04-11 14:03:19.020026"], ["id", 3]]
  PaperTrail::Version Create (0.8ms)  INSERT INTO "versions" ("item_type", "item_id", "event", "whodunnit", "object", "object_changes", "locale", "created_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"  [["item_type", "Amusement"], ["item_id", 3], ["event", "update"], ["whodunnit", "1"], ["object", "---\ngallery: '[\"screen-2020-04-11-12-42-10.jpg\"]'], ["object_changes", "---\ngallery:\n- '[\"screen-2020-04-11-12-42-10.jpg\"]'\n- '[\"screen-2020-04-11-12-42-10.jpg\"]'\n"], ["locale", "ru"], ["created_at", "2020-04-11 14:03:19.020026"]]
   (24.7ms)  COMMIT
Redirected to http://localhost:3000/admin/amusement
Completed 302 Found in 205ms (ActiveRecord: 38.2ms | Allocations: 30207)

Or even if I just use irb:

irb(main):007:0> a = Amusement.last
irb(main):008:0> a[:gallery]
=> ["grapefruit-slice-332-332.jpg"]
irb(main):009:0> a.gallery = ["grapefruit-slice-332-332.jpg"]
irb(main):010:0> a.changed?
=> true
irb(main):011:0> a.changes
=> {"gallery"=>[["grapefruit-slice-332-332.jpg"], ["grapefruit-slice-332-332.jpg"]]}
irb(main):012:0> a.save!
Amusement Update (1.3ms)  UPDATE "amusements" SET "gallery" = $1, "updated_at" = $2 WHERE "amusements"."id" = $3  [["gallery", "[\"grapefruit-slice-332-332.jpg\"]"], ["updated_at", "2020-04-11 14:11:16.310387"], ["id", 4]]
PaperTrail::Version Create (1.4ms)  INSERT INTO "versions" ("item_type", "item_id", "event", "object", "object_changes", "locale", "created_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["item_type", "Amusement"], ["item_id", 4], ["event", "update"], ["object", "---\ngallery: '[\"grapefruit-slice-332-332.jpg\"]'], ["object_changes", "---\ngallery:\n- '[\"grapefruit-slice-332-332.jpg\"]'\n- '[\"grapefruit-slice-332-332.jpg\"]'\n"], ["locale", "ru"], ["created_at", "2020-04-11 14:11:16.310387"]]

Does anyone know how to fix it properly?

@airled airled changed the title Carrierwave hit update with unchanged value Carrierwave hits update with unchanged value Apr 11, 2020
@mshibuya mshibuya added this to the Release v3.0.0 milestone Jan 14, 2023
mshibuya added a commit that referenced this issue Feb 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants