title: Rails 4.2.x - use params hash data in model
body: <p><strong>QUESTION</strong> - is there a way to pass/use <code>params</code> hash data to the associated model?</p>
  
  <p>Essentials of my app:</p>
  
  <ol>
  <li><code>Image</code> model <code>belongs_to</code> <code>User</code> model, <code>User</code> model <code>has_many</code> <code>Image</code> instances.  </li>
  <li><p><code>Image</code> schema:</p>
  
  <p>create_table "images", force: :cascade do |t|
      t.string   "filename"
      t.string   "mime_type"
      t.binary   "content"
      t.integer  "user_id"
      t.datetime "created_at", null: false
      t.datetime "updated_at", null: false
    end</p>
  
  <p>add_index "images", ["user_id"], name: "index_images_on_user_id"</p></li>
  <li><p>In root_url (which is <code>StaticPagesController#home</code>) I have image upload form:</p>
  
  </li>
  <li><p>in <code>Image</code> model I do:</p>
  
  <pre><code>def uploaded_file=(initial_data)
    self.filename = initial_data.original_filename
    self.mime_type = initial_data.content_type
    self.content = initial_data.read
    initial_data.rewind
  end
  </code></pre></li>
  <li><p>Also a custom validation:</p>
  
  <pre><code>validate :mime_type
  </code></pre></li>
  </ol>
  
  <p>with </p>
  
  <pre><code>def mime_type
      correct_mime = ["image/jpeg", "image/png", "image/gif"]
      unless correct_mime.include? params[:image][:uploaded_file].content_type.chomp
          errors.add(:base, 'must be .jpeg, .png or .gif')
      end
  end
  </code></pre>
  
  <ol start="5">
  <li>As it is known all query from upload form goes to params[:image][:uploaded_file]</li>
  </ol>
  
  <p><strong>6. Is it possible to pass params[:image][:uploaded_file] to <code>Image</code> model as a hash with original hash structure?</strong></p>
  
  <p>What I have tried and none of them works:</p>
  
  <ul>
  <li>pass explicit params hash right in <code>Image</code> model - NO GO</li>
  <li>define instance @params_hash = params[:image][:uploaded_file] in <code>create</code> action (separately or from inside the custom class method) - NO GO</li>
  <li>Define constant in <code>controller#action</code> - NO GO</li>
  </ul>
  
  <p>WHAT works??
  Global variable - <code>$variable</code></p>
  
  <p>Is it a rails way to do that? -> query data to model</p>