Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8852365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:22:52+00:00 2026-06-14T13:22:52+00:00

Im trying to create an dynamic bucket name depending on my polymorphic association type.

  • 0

Im trying to create an dynamic bucket name depending on my polymorphic association type.

My first approach was trying something like this:

class PostImage < ActiveRecord::Base
  belongs_to :imageable, :polymorphic => true

  has_attached_file :image, :styles => { :small => "200x200>", :thumb => "50x50>" },
                            :storage => :s3, 
                            :s3_credentials => "#{Rails.root}/config/s3.yml", 
                            :path => "/:style/:id/:filename", 
                            :bucket => self.imageable_type.to_s

end

If i try to create a new object i got the next error:

NoMethodError: undefined method `imageable_type’ for #< Class:0x007fd3fe0b15d8

I find out on the S3 documentation this:

bucket: This is the name of the S3 bucket that will store your files. Remember that the bucket must be unique across all of Amazon S3. If the bucket does not exist Paperclip will attempt to create it. The bucket name will not be interpolated. You can define the bucket as a Proc if you want to determine it’s name at runtime. Paperclip will call that Proc with attachment as the only argument.

The problem is that i don’t get how i can get this working to set the name of my polymorphic association as the name of the bucket.

Any help will be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:22:54+00:00Added an answer on June 14, 2026 at 1:22 pm

    Hope it help somebody,

    The final solution was based on this post: rails paperclip S3 with dynamic bucket name

    Read the post for get a better explanation of how use the Proc.

    The final code:

    class PostImage < ActiveRecord::Base
      belongs_to :imageable, :polymorphic => true
    
      has_attached_file :image, :styles => {
                       :square=> "170x170#",
                       :rectangle=> "220x170#",
                       :large  => "600x400#" }, :storage => :s3, :s3_credentials => "#{Rails.root}/config/s3.yml",
                       :path => "/:style/:id/:filename",
                       :bucket =>  lambda { |attachment| "#{attachment.instance.imageable_type.to_s.downcase}-gallery" }
    
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create dynamic subdomains in .htaccess. I want something like this:
i'm trying to create a Dynamic Sub-menu to use it in Wordpress, Something like
I am trying to create a dynamic class from a symbol in an embedded
I am trying to create a dynamic advanced search with Search field for name
I am trying to create a dynamic array of type 'T' that contains an
I'm trying to create a dynamic drop down menu that pulls the city name
this is my javascript code , I am trying to create a dynamic list
I'm trying to create a dynamic array in my List class that will start
I'm trying to create a dynamic CheckBoxFor where some items are disabled. This is
I am trying to create dynamic pages without creating new files and getting the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.