1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activestorage/test
Abhishek Chandrasekhar 32438ed64f [ActiveStorage] Ensure that the _blob association is properly loaded when attaching ::One
Consider a model with `One` and `Many` attachments configured:

    class User < ActiveRecord::Base
      has_one_attached :avatar
      has_many_attached :highlights
    end

=== One Attachment

After attaching `One` attachment (`:avatar`), we can see that the associated
`_blob` record (`:avatar_blob`) still returns as `nil`.

    user.avatar.attach(blob)
    user.avatar_attachment.present?  => true
    user.avatar_blob.present?        => false    # Incorrect!

This is a false negative. It happens because after the attachment and blob
are built:

  1. The record already has its `_blob` association loaded, as `nil`
  2. the `::Attachment` is associated with the record but the `::Blob` only gets
    associated with the `::Attachment`, not the record itself

In reality, the blob does in fact exist. We can verify this as follows:

    user.avatar.attach(blob)
    user.avatar_attachment.blob.present?    => true  # Blob does exist!

The fix in this change is to simply assign the `::Blob` when assigning
the `::Attachment`. After this fix is applied, we correctly observe:

    user.avatar.attach(blob)
    user.avatar_attachment.present?  => true
    user.avatar_blob.present?        => true    # Woohoo!

=== Many Attachments

We don't see this issue with `Many` attachments because the `_blob` association
is already loaded as part of attaching more/newer blobs.

    user.highlights.attach(blob)
    user.highlights_attachments.any?    => true
    user.highlights_blobs.any?          => true
2019-02-26 11:10:38 -05:00
..
analyzer Move #extract_metadata_from to ActiveStorage's TestHelper 2018-04-25 15:28:30 -05:00
controllers Fix ArgumentError: Unsafe redirect 2019-01-21 14:15:34 +09:00
database
dummy Merge pull request #34132 from ConfusedVorlon/enable_fragment_cache_log_in_dev 2019-01-08 14:15:16 -05:00
fixtures/files Permit generating variants of TIFF images 2018-12-30 18:14:49 -05:00
jobs Remove superfluous test 2018-08-19 01:10:50 -04:00
models [ActiveStorage] Ensure that the _blob association is properly loaded when attaching ::One 2019-02-26 11:10:38 -05:00
previewer Change video preview format from PNG to JPG 2018-05-23 14:32:34 -04:00
service Delegated path_for to primary in the MirrorService 2019-02-14 23:06:42 +05:30
template Use assert_predicate and assert_not_predicate 2018-01-25 23:32:59 -05:00
test_helper.rb use "minitest" consistently 2018-09-11 23:57:25 +02:00