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

The Archive Base Latest Questions

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

I am trying to create a separate model class for image uploading, which was

  • 0

I am trying to create a separate model class for image uploading, which was previously in the object’s controller. I also want to make it agnostic, so I can upload images using one class from multiple objects.

In my original object’s model class, I have the following now:

class Object < ActiveRecord::Base
  after_save :photo_store
  delegate :has_photo?, :photo, :photo_path, :store_photo, :photo_filename, :to => :photo_store

  def photo_store
    PhotoStore.new(self)
  end
end

Then, the PhotoStore class looks like this:

class PhotoStore
  attr_reader :object

  def initialize(object)
    @object = object
  end

  def photo=(file_data)
    unless file_data.blank?
      @file_data = file_data
      self.extension = file_data.original_filename.split('.').last.downcase
    end
  end

  PHOTO_STORE = File.join RAILS_ROOT, 'public', 'photo_store'

  def photo_filename
    File.join PHOTO_STORE, "#{id}.#{extension}"
  end

  def photo_path
    "/photo_store/#{id}.#{extension}"
  end

  def has_photo?
    File.exists? photo_filename
  end

  private

  def store_photo
    if @file_data
      FileUtils.mkdir_p PHOTO_STORE
      File.open(photo_filename, 'wb') do |f|
        f.write(@file_data.read)
      end
    end
  end
end

However, this throws the error below when I try and use the has_photo? method in the object’s view.

undefined local variable or method `id’ for #

Do I need to put some other type of relationship in place between the Object and PhotoStore?

And a separate question: What’s the best way to make this agnostic? Since it uses just the ID of the object, I could just include the Object’s name in the filename, but is that the best way to do it?

Thanks!

  • 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-22T15:21:53+00:00Added an answer on May 22, 2026 at 3:21 pm

    Because at File.join PHOTO_STORE, "#{id}.#{extension}" you call method PhotoStore#id, but it does not exists.

    You should do that

    File.join PHOTO_STORE, "#{@object.id}.#{@object.extension}"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a bookmarklet for posting del.icio.us bookmarks to a separate account.
Trying to create a user account in a test. But getting a Object reference
I'm trying to create a webapplication where I want to be able to plug-in
I am trying create a WCF service that leverages the WPF MediaPlayer on the
Trying to create a QtRuby application, I get the following error: /usr/lib64/ruby/site_ruby/1.8/Qt/qtruby4.rb:2144: [BUG] Segmentation
Trying to create my first iPhone app that would play back audio. When I
Trying to create several layers of folders at once C:\pie\applepie\recipies\ without using several different
Trying to create a small monitor application that displays current internet usage as percentage
Trying to create a list to return some JSON data to a view. Following
I'm trying to create a sitemap using Linq to Xml, but am getting an

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.