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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:01:40+00:00 2026-06-10T09:01:40+00:00

I’m writing my first RoR app and currently I’m working on allowing users to

  • 0

I’m writing my first RoR app and currently I’m working on allowing users to upload images. I’m using Paperclip for this purpose. One of the steps involves adding has_attached_file to my model:

class MyModel < ActiveRecord::Base
  #...
  has_attached_file :picture, styles: {
    large: "120x150#>", 
    small: "60x75#>"
  }
  #...
end

If I do it like this, everything works smoothly (or so it seems). But I will also need to access the same constant values as integers somewhere else, so I’ve added a hash:

class MyModel < ActiveRecord::Base
  #...
  has_attached_file :picture, styles: {
    large: "120x150#>", 
    small: "60x75#>"
  }
  def picture_sizes
  {
    large: {width: 120, height: 150},
    small: {width: 60, height: 75}
  }
  end
  #...
end

This creates an ugly redundancy. So I thought about writing a method generating the first hash from the second one, like this

class MyModel < ActiveRecord::Base
  #...
  has_attached_file :picture, styles: picture_sizes_as_strings

  def picture_sizes
  {
    large: {width: 120, height: 150},
    small: {width: 60, height: 75}
  }
  end

  def picture_sizes_as_strings
    result = {}
    picture_sizes.each do |label, size|
      result[:label] = "#{size[:width]}x#{size[:height]}#>"
    end
    return result
  end
  #...
end

But this rises an error:

undefined local variable or method `picture_sizes_as_strings' for #<Class:0x007fdf504d3870>

What am I doing wrong?

  • 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-10T09:01:42+00:00Added an answer on June 10, 2026 at 9:01 am

    The problem is that you’re trying to use an instance method picture_sizes_as_strings in a declaration (has_attached_image) that’s run on the class level. It’s the difference between calling

    MyModel.picture_sizes_as_strings
    

    and

    MyModel.first.picture_sizes_as_strings
    

    In the first case we are referring to a class method (a method on the class MyModel itself) and in the second case we are referring to an instance method (a method on the individual my_model object.)

    So first of all you have to change the methods to class methods by prefixing their names with self., so:

    def self.picture_sizes
      {
        large: {width: 120, height: 150},
        small: {width: 60, height: 75}
      }
    end
    

    Now that doesn’t completely fix your problem yet, because has_attached_image is processed when the model is first parsed by ruby. That means it will try to run has_attached_image before you define self.picture_sizes so it will still say undefined method.

    You can fix this by putting self.picture_sizes before the has_attached_file declaration but that’s quite ugly. You could also put the data in a constant but that has its own problems.

    Honestly there’s no really pretty way to fix this. If it were me I’d probably reverse the whole process, define the styles as normal and then use a method to convert the strings to integers, something like this:

    class MyModel < ActiveRecord::Base
      has_attached_file :picture, styles: {
        large: "120x150#>", 
        small: "60x75#>"
      }
    
      def numeric_sizes style
        # First find the requested style from Paperclip::Attachment
        style = self.picture.styles.detect { |s| s.first == style.to_sym }
    
        # You can consolidate the following into one line, I will split them for ease of reading
        # First strip all superfluous characters, we just need the numerics and the 'x' to split them
        sizes = style.geometry.gsub(/[^0-9x]/,'')
        # Next split the two numbers across the 'x'
        sizes = sizes.split('x')
        # Finally convert them to actual integer numbers
        sizes = sizes.map(&:to_i)
      end
    end
    

    Then you can call MyModel.first.numeric_sizes(:medium) to find out the sizes for a specific style, returned as an array. Of course you could change them into a hash too or whatever format you need.

    • 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
We're building an app, our first using Rails 3, and we're having to build
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am writing an app with both english and french support. The app requests
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

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.