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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:39:50+00:00 2026-05-19T23:39:50+00:00

i have an attribute called dimensions which i want to set based on my

  • 0

i have an attribute called dimensions which i want to set based on my width, height, and depth attributes.

for example, i want to do ShippingProfile.find(1).width = 4, and have it save to dimensions as {:width => 4, :height => 0, :depth => 0}`

is this possible?

class ShippingProfile < ActiveRecord::Base
  after_initialize :set_default_dimensions

  serialize :dimensions, Hash

  attr_accessor :width, :height, :depth
  attr_accessible :width, :height, :depth, :dimensions

  private

    def set_default_dimensions
      self.dimensions ||= {:width => 0, :height => 0, :depth => 0}
    end  
end
  • 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-19T23:39:51+00:00Added an answer on May 19, 2026 at 11:39 pm

    Very much so, all you need to do is use a callback to set the value of self.dimensions:

    class ShippingProfile < ActiveRecord::Base
      after_initialize :set_default_dimensions
      after_validation :set_dimensions
    
      serialize :dimensions, Hash
    
      attr_accessor :width, :height, :depth
      attr_accessible :width, :height, :depth, :dimensions
    
      private
    
      def set_default_dimensions
        self.dimensions ||= {:width => 0, :height => 0, :depth => 0}
      end
    
      def set_dimensions
        self.dimensions = { 
          :width  => self.width || self.dimensions[:width],
          :height => self.height || self.dimensions[:height],
          :depth  => self.depth || self.dimensions[:depth],
        }
      end
    end
    

    You need to use self.foo || self.dimensions[:foo] to ensure that you preserve any existing values already set in the hash. Why? Your dimension attributes (I’m assuming) aren’t being persisted in the database – you’re using attr_accessor, rather than setting them up as fields in your table.

    As an aside, I think you’re going about your model design the wrong way. By storing the dimensions as a hash in the database, not only do you lose the ability to query based on those attributes, but you add a level of fragility you don’t need.

    If you are storing your individual dimension attributes as separate fields, then you’re introducing redundancy and complexity. You would be better served by having the three attributes as fields in your database (if you aren’t already), then generating the dimensions hash on the fly when it’s needed:

    class ShippingProfile < ActiveRecord::Base
      def dimensions
        { :width => self.width, :height => self.height, :depth => self.depth }
      end
    end
    

    This way, you retain the functionality and gain some flexibility.

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

Sidebar

Related Questions

I have a attribute called: description and I want to have the following in
In my user model, I have an attribute called nickname and validates as such:
If I have a class called animal, dog and fish is the subclass. The
I have a function resizePreview() that will resize an image in a jQuery dialog
I have a question about manually creating a .Net webservice proxy class. The WSDL
How can I query those of the labels in ClearCase with cleartool that have
I'm having a hard time understanding what I'm supposed to do. The only thing
I'm still learning the ropes of OLAP, cubes, and SSAS, but I'm hitting a
I was answering some quiz questions for an interview, and the question was about
I am looking to aggregate values for all versions of a slowly changing dimension.

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.