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

Fix ActiveStorage::Blob inverse association: #50800

Merged
merged 1 commit into from
Jan 21, 2024

Conversation

Edouard-chin
Copy link
Member

Motivation / Background

This is a fix needed to unblock #50284, because Active Storage relies on a Active Record bug.

Detail

The problem is best understood with a small snippet:

blob = ActiveStorage::Blob.new

ActiveStorage::Attachment.new(blob: blob)
ActiveStorage::Attachment.new(blob: blob)

# Currently:
p blob.attachments #=> #<ActiveRecord::Associations::CollectionProxy []>

# Once the Active Record bug is fixed:
p blob.attachments #=> #<ActiveRecord::Associations::CollectionProxy [#<ActiveStorage::Attachment id: nil, name: nil, record_type: nil, record_id: nil, blob_id: nil, created_at: nil>, #<ActiveStorage::Attachment id: nil, name: nil, record_type: nil, record_id: nil, blob_id: nil, created_at: nil>]>

# Saving the blob would result in trying to create 2 attachments which
# fails because of unique constraints.

Additional information

The real code path that does what the snippet above does is located here:

record.public_send("#{name}=", blobs + attachables.flatten)

It's basically doing this:

user.images.attach "photo1.png"
# Initialize a Blob record and an Attachment

user.images.attach "photo2.png"
# Get the Blob from above, create another Attachment
# Initialize a new Blob record and an new Attachment

# Rinse and repeat every time `attach` is called

Solution

Tests

I didn't add tests, the test suite is already well covered, adding the inverse_of
without any changes breaks the test suite already. You can try by running
for instance the activestorage/test/models/attached/many_test.rb.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

- This is a fix needed to unblock
  rails#50284,
  because Active Storage relies on a Active Record bug.

  The problem is best understood with a small snippet:

  ```
    blob = ActiveStorage::Blob.new

    ActiveStorage::Attachment.new(blob: blob)
    ActiveStorage::Attachment.new(blob: blob)

    # Currently:
    p blob.attachments #=> #<ActiveRecord::Associations::CollectionProxy []>

    # Once the Active Record bug is fixed:
    p blob.attachments #=> #<ActiveRecord::Associations::CollectionProxy [#<ActiveStorage::Attachment id: nil, name: nil, record_type: nil, record_id: nil, blob_id: nil, created_at: nil>, #<ActiveStorage::Attachment id: nil, name: nil, record_type: nil, record_id: nil, blob_id: nil, created_at: nil>]>

    # Trying to save the blob would result in trying to create 2 attachments which
    # fails because of unique constrainsts.
  ```

  ### Code path

  The real code path that does what the snippet above does is located here:

  https://github.com/rails/rails/blob/9c3ffab47c3bf59320ba08e9dafdb0275cf91a5a/activestorage/lib/active_storage/attached/many.rb#L52

  It's basically doing this:

  ```
    user.images.attach "photo1.png"
    # Initialize a Blob record and an Attachment

    user.images.attach "photo2.png"
    # Get the Blob from above, create another Attachment
    # Initialize a new Blob record and an new Attachment

    # rinse and repeat every time `attach` is called
  ```

  Basically each time we call `attach`, we grab the previous blobs that were attached
  (and that already have an Attachment record), and

  ### Solution

  - Explicitly set the `inverse_of`, so that it behaves as if rails#50284 is shipped
  - Don't build a new attachment for blob already having one.

  ### Tests

  I didn't add tests, the test suite is already well covered, adding the `inverse_of`
  without any changes breaks the test suite already. You can try by running
  for instance the `activestorage/test/models/attached/many_test.rb`.
@byroot byroot merged commit 310bb2a into rails:main Jan 21, 2024
3 of 4 checks passed
casperisfine pushed a commit to Shopify/rails that referenced this pull request Jan 26, 2024
Ref: rails#50284

While having the inverse association configured it generally positive
as it avoid some extra queries etc, infering it may break legecy code,
as evidenced by how it broke `ActiveStorage::Blob` in rails#50800

As such we can't just enable this behavior immediately, we need to provide
and upgrade path for users.
casperisfine pushed a commit to Shopify/rails that referenced this pull request Mar 21, 2024
Ref: rails#50284

While having the inverse association configured it generally positive
as it avoid some extra queries etc, infering it may break legecy code,
as evidenced by how it broke `ActiveStorage::Blob` in rails#50800

As such we can't just enable this behavior immediately, we need to provide
and upgrade path for users.
casperisfine pushed a commit to Shopify/rails that referenced this pull request Mar 21, 2024
Ref: rails#50284

While having the inverse association configured it generally positive
as it avoid some extra queries etc, infering it may break legecy code,
as evidenced by how it broke `ActiveStorage::Blob` in rails#50800

As such we can't just enable this behavior immediately, we need to provide
and upgrade path for users.
casperisfine pushed a commit to Shopify/rails that referenced this pull request Mar 21, 2024
Ref: rails#50284

While having the inverse association configured it generally positive
as it avoid some extra queries etc, infering it may break legecy code,
as evidenced by how it broke `ActiveStorage::Blob` in rails#50800

As such we can't just enable this behavior immediately, we need to provide
and upgrade path for users.
casperisfine pushed a commit to Shopify/rails that referenced this pull request Mar 21, 2024
Ref: rails#50284

While having the inverse association configured it generally positive
as it avoid some extra queries etc, infering it may break legecy code,
as evidenced by how it broke `ActiveStorage::Blob` in rails#50800

As such we can't just enable this behavior immediately, we need to provide
and upgrade path for users.
casperisfine pushed a commit to Shopify/rails that referenced this pull request Mar 22, 2024
Ref: rails#50284

While having the inverse association configured it generally positive
as it avoid some extra queries etc, infering it may break legecy code,
as evidenced by how it broke `ActiveStorage::Blob` in rails#50800

As such we can't just enable this behavior immediately, we need to provide
and upgrade path for users.
casperisfine pushed a commit to Shopify/rails that referenced this pull request Mar 25, 2024
Ref: rails#50284

While having the inverse association configured it generally positive
as it avoid some extra queries etc, infering it may break legecy code,
as evidenced by how it broke `ActiveStorage::Blob` in rails#50800

As such we can't just enable this behavior immediately, we need to provide
and upgrade path for users.
fractaledmind pushed a commit to fractaledmind/rails that referenced this pull request May 13, 2024
Ref: rails#50284

While having the inverse association configured it generally positive
as it avoid some extra queries etc, infering it may break legecy code,
as evidenced by how it broke `ActiveStorage::Blob` in rails#50800

As such we can't just enable this behavior immediately, we need to provide
and upgrade path for users.
xjunior pushed a commit to xjunior/rails that referenced this pull request Jun 9, 2024
Ref: rails#50284

While having the inverse association configured it generally positive
as it avoid some extra queries etc, infering it may break legecy code,
as evidenced by how it broke `ActiveStorage::Blob` in rails#50800

As such we can't just enable this behavior immediately, we need to provide
and upgrade path for users.
HeyNonster pushed a commit to HeyNonster/rails that referenced this pull request Jun 12, 2024
Ref: rails#50284

While having the inverse association configured it generally positive
as it avoid some extra queries etc, infering it may break legecy code,
as evidenced by how it broke `ActiveStorage::Blob` in rails#50800

As such we can't just enable this behavior immediately, we need to provide
and upgrade path for users.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants