How to determine active storage association?

active storage url_for
active storage s3
rails_blob_path
rails active storage tutorial
active storage direct upload
active storage content-type
active storage example
storage rails

I have one upload form for all models using active storage attachment(s). Functionality in this form can change depending on if the uploader is a has_one_attached or has_many_attached. Is there a rails way to determine which association type a given model has?

You can use reflect_on_all_associations:

https://api.rubyonrails.org/classes/ActiveRecord/Reflection/ClassMethods.html#method-i-reflect_on_all_associations

Account.reflect_on_all_associations   # returns an array of all associations
Account.reflect_on_all_associations(:has_many)  # returns an array of all has_many associations

I've not tried it but I'm assuming:

Model.reflect_on_all_associations(:has_one_attached)

Will contain a record if the model :has_one_attached, and be empty if it doesn't.

Active Storage Overview — Ruby on Rails Guides, Learn how to set up Active Storage in a Rails 6 app and use it to If you want to see Active Storage in action in a complete Rails app, check out our blob (an uploaded image), but we still need to declare associations in our� See Active Storage in Action! To see Active Storage used in the context of a full-featured app, give our Ruby on Rails 6 course a try. You'll discover how to pragmatically put all the pieces together to build and design Rails apps step-by-step.

I don't love this approach, but it works. All my models have only one attachment association. If a model had more than one attachment association, this would break. I'll continue to fine tune this.

def can_have_many_attachments?
  uploader_name.present? &&
    uploader_name.ends_with?('s')
end

def uploader_name
  self.class.reflect_on_all_attachments.last.name.to_s
end

Eager loading Active Storage models to avoid N+1 issues, Rails 5.2 and Active Storage new way to upload and attach files. Let's check out what needs to be done when adapting your project to After migration, you need to add associations to attachments in your model:. The Self Storage Association (SSA) is a not-for-profit tax-exempt organization formed in 1975 under Section 501-c-6 of the Internal Revenue Code. For over 40 years, the Association has served as the official trade organization and voice of the U.S. and international self storage industry.

The following seems to work, at least for Active Storage.

ModelName.reflect_on_all_associations.select { |a|
  a.class_name == 'ActiveStorage::Attachment'
}

And if you want just the attribute names:

ModelName.reflect_on_all_associations.select { |a|
  a.class_name == 'ActiveStorage::Attachment'
}.map(&:name)

Using Active Storage in Rails 6, Check out the official Rails announcement blog. Active Storage is the Rails way of storing your static files without the need of any 3rd party gems. file attachment as well as the record, which is also a polymorphic association. The Self Storage Association (SSA) is a not-for-profit tax-exempt organization formed in 1975 under Section 501-c-6 of the Internal Revenue Code. For over 40 years, the Association has served as the official trade organization and voice of the U.S. and international self storage industry.

Rails 5.2 and Active Storage, How to add ActiveStorage to your Ruby on Rails 5.2+ project and active_storage_attachments (a polymorphic association) will be the link the database, we now have to tell ActiveStorage where to store uploaded files. Azure Active Directory: Use Azure Active Directory (Azure AD) credentials to authenticate a user, group, or other identity for access to blob and queue data. If authentication of an identity is successful, then Azure AD returns a token to use in authorizing the request to Azure Blob storage or Queue storage.

Rails 5: Getting Started with Active Storage, Active Storage uses polymorphic associations via the Attachment join model, which then connects to the actual Blob. https://github.com/rails/rails/blob/master/ activestorage/README.md# I couldn't figure out why, until. The Self Storage Association (SSA) is a not-for-profit tax-exempt organization formed in 1975 under Section 501-c-6 of the Internal Revenue Code. For over 40 years, the Association has served as the official trade organization and voice of the U.S. and international self storage industry.

How to use ActiveStorage in your Rails 5.2+ application, “Active Storage uses polymorphic associations via the Attachment join use attached? to check presence --> <p> <strong>Image:</strong>� The Self Storage Association (SSA) is a not-for-profit tax-exempt organization formed in 1975 under Section 501-c-6 of the Internal Revenue Code. For over 40 years, the Association has served as the official trade organization and voice of the U.S. and international self storage industry.

Comments
  • reflect_on_all_associations does not include attachments. However, rails has two other methods "reflect_on_all_attachments", and "reflect_on_attachment". They both return a ActiveStorage::Reflection object....from which I am unsure how to determine if that reflection is a has_one or a has_many.
  • I could do something like Document.reflect_on_attachment(:attachments).name.ends_in?('s'). But, that seems awfully brittle.
  • It would be pretty ugly but you could check if the association returned is singular or plural
  • FWIW I tried Model.reflect_on_all_associations(:has_one_attached) and it does not seem to work.