Skip to content

The ActiveRecord "_changed?" method returns false after assigning a file with the same file name but different content #2719

@bli

Description

@bli

With an existing active record with an existing file, if you assign a new version of the file (with the same file name but different content), the "_changed?" method returns false instead true.

To demonstrate this, we can add a test in activerecord_spec.rb, just after the test "should mark image as changed when saving a new image".

      it "should mark image as changed when saving a new image with same file name" do
        @event.image = stub_file("test.jpeg")
        @event.save
        @event.image = stub_file_alt("test.jpeg")
        expect(@event.image_changed?).to be_truthy
        @event.save
        expect(@event.reload.image.read).to eq "this is new stuff\n"
      end

Here the stub_file_alt is similar to stub_file. The difference is that it gets the file from another directory (so that we can have a different fixture with the same file name).

      def stub_file_alt(filename, mime_type=nil, fake_name=nil)
        f = File.open(file_path("alternatives", filename))
        allow(f).to receive(:content_type) { mime_type } if mime_type
        f
      end

The test failed the first expect. However the second expect would pass should we comment out the first one.

Note that this is the same scenario mentioned in #2712. But the issue here is the "_changed?", ".changes" methods do not reflect the fact that the content of the file has been changed.

This seems to be an issue since version 3.0.0.rc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions