title: rails paperclip S3 with dynamic bucket name
body: <p>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.</p>
  
  <p>Therefore, in my controller, I have this:</p>
  
  <pre><code> def new
      @pmdocument = Pmdocument.new
      @pmdocument.projectmilestone_id=params[:projectmilestone_id]
  </code></pre>
  
  <p>where projectmilestone_id is the foreign_key to my project (to be used as my bucket name)</p>
  
  <p>My model is like this:</p>
  
  <pre><code>class Pmdocument &lt; 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 =&gt; false
    has_attached_file :pmdoc,
      :storage =&gt; :s3,
      :bucket =&gt; self.projectmilestone_id.to_s,
      :s3_credentials =&gt; File.join(Rails.root, 'config', 's3.yml')
  </code></pre>
  
  <p>When I load the page, I get this error: 
  undefined method `projectmilestone_id' for #</p>
  
  <p>I checked my controller and the projectmilestone_id field is correctly loaded there.</p>
  
  <p>I tried to change the bucket line to <code>:bucket =&gt; self.name</code> and then the error is gone.</p>
  
  <p>The model works ok because projectmilestone_id is correctly stored in the db.</p>
  
  <p>My guess is that it could be linked to the accessible attributes but it seems to be ok too.</p>
  
  <p>What's wrong?  Many thanks!!!</p>
  
  <hr>
  
  <p>I really dont get it:</p>
  
  <p>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.</p>
  
  <p>This is the code:</p>
  
  <pre><code>:path =&gt; proc { |attachment| "#{attachment.istance.projectname}/:attachment/:id/:basename.:extension" },
  </code></pre>
  
  <p>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.</p>
  
  <p>Any clue? </p>