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

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

Closed
bli opened this issue Jan 15, 2024 · 1 comment
Labels

Comments

@bli
Copy link

bli commented Jan 15, 2024

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.

@mshibuya mshibuya added the bug label Feb 4, 2024
@IsaacConDosA
Copy link

IsaacConDosA commented Feb 27, 2024

The fix didn't work for me :(. In 1.x and 2.x it works

My code is

book.author.remote_image_url = new_uri
book.author.changed? # false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants