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 4623512
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:59:52+00:00 2026-05-22T02:59:52+00:00

I’m using paperclip to upload my documents to Amazon S3. I would like to

  • 0

I’m using paperclip to upload my documents to Amazon S3. I would like to automatically create a bucket with the ID of my projects when I upload a new document.

Therefore, in my controller, I have this:

 def new
    @pmdocument = Pmdocument.new
    @pmdocument.projectmilestone_id=params[:projectmilestone_id]

where projectmilestone_id is the foreign_key to my project (to be used as my bucket name)

My model is like this:

class Pmdocument < ActiveRecord::Base
  belongs_to :projectmilestone
  attr_accessible :id, :name, :description, :projectmilestone_id, :pmdoc, :projectmilestone_attributes
  attr_protected :pmdoc_content_type, :pmdoc_size
  accepts_nested_attributes_for :projectmilestone, :allow_destroy => false
  has_attached_file :pmdoc,
    :storage => :s3,
    :bucket => self.projectmilestone_id.to_s,
    :s3_credentials => File.join(Rails.root, 'config', 's3.yml')

When I load the page, I get this error:
undefined method `projectmilestone_id’ for #

I checked my controller and the projectmilestone_id field is correctly loaded there.

I tried to change the bucket line to :bucket => self.name and then the error is gone.

The model works ok because projectmilestone_id is correctly stored in the db.

My guess is that it could be linked to the accessible attributes but it seems to be ok too.

What’s wrong? Many thanks!!!


I really dont get it:

I decided not to change my bucket anymore (bad idea anyway as the name need to be unique for all S3) but to change my path instead.

This is the code:

:path => proc { |attachment| "#{attachment.istance.projectname}/:attachment/:id/:basename.:extension" },

The first folder with my project name is not created. If I replace projectname by name, or even description (another field of pmdocuments), it works, but not with projectname. Of course, I checked that projectname is correctly populated. The reason is elsewhere.

Any clue?

  • 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-05-22T02:59:53+00:00Added an answer on May 22, 2026 at 2:59 am

    The has_attached_file method is executed in the context of the class (when the file is loaded), not in the context of a record instance where you could use attributes and other instance method. self.name works indeed, but it returns the name of the class ("Pmdocument"), not the name of a record.

    But Paperclip is kind enough to allow what you want. The documentation on the S3 storage says:

    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.

    In your case it would be something like this:

    has_attached_file :pmdoc,
      :storage => :s3,
      :bucket => proc { |attachment| attachment.instance.projectmilestone_id.to_s },
      :s3_credentials => File.join(Rails.root, 'config', 's3.yml')
    

    Now you pass a Proc to has_attached_file. The content of the block is not evaluated while your class is loaded, but later when it’s needed. Paperclip then calls the block with the attachment as argument and uses the returned value as bucket name.

    Edit:

    Unfortunately, this block is run when the file is assigned, not when the record is saved. So all your attributes may not be set yet (the order of the assignment of the attributes when you do Pmdocument.new(params[:pmdocument]) is undetermined). I’d like Paperclip to work another way, but in the meantime I see 2 options:

    You can remove the file from the params in the controller and set it when everything else is ready:

    pmdoc = params[:pmdocument].delete(:pmdoc)
    @pmdocument = Pmdocument.new(params[:pmdocument])
    @pmdocument.pmdoc = pmdoc
    

    Or you can delay Paperclip post-processing by disabling it with before_post_process (see the Events section of the README) and running it in an after_save callback.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Paperclip to handle profile photo uploads in my app. They upload
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I've got a string that has curly quotes in it. I'd like to replace

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.