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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:36:51+00:00 2026-05-26T04:36:51+00:00

I need to compare some Rails (2.3.11) model attribute values before and after a

  • 0

I need to compare some Rails (2.3.11) model attribute values before and after a database update, so I start by finding my record and saving the existing attribute values in a hash, as follows:

id = params[:id]
work_effort = WorkEffort.find(id)

ancestor_rollup_fields = {
    :scheduled_completion_date => work_effort.scheduled_completion_date
}

work_effort.update_attributes(params.except(:controller, :action))
#etcetera

Note I am adhering to the “best practice” of using a symbol for a hash key.

Then I have a method that takes the model and the hash to determine possible additional steps to take if the values from the hash and the model attributes don’t match. To determine this I tried to get at the model attribute value in an each loop but I was getting nil at first:

def rollup_ancestor_updates(work_effort, ancestor_rollup_fields)
    ancestor_rollup_fields.each do |key, value|
        model_val = work_effort.attributes[key] #nil
        #etcetera

In debugging the above I noticed that hard-coding a string as a key:

work_effort.attribute['scheduled_completion_date']

Returned the desired value. So then in my each block I tried the following and it worked:

model_val = work_effort.attributes[key.to_s]

Is there a different way to do this? To me, with just 3 months Ruby/Rails experience, it’s confusing to use symbols as hash keys as is the prescribed best practice, but then have to call .to_s on the symbol to get at a model attribute. Has anybody else experienced this, worked around this, been confused by this too? Thanks in advance

  • 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-26T04:36:52+00:00Added an answer on May 26, 2026 at 4:36 am

    The Hash returned when you call #attributes on a AR instance has string keys, which is why a symbol as an index into the hash doesn’t work in your case. There is a subclass of Hash called HashWithIndifferentAccess which automatically converts symbol indexes into strings.

    Quite often in Rails you’ll encounter HashWithIndifferentAccess instances. A perfect example is the params variable you access in your controller and view code.

    Try using work_effort.attributes.with_indifferent_access[key]

    Really it is just doing the same thing that you are, but it does it behind the scenes.

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

Sidebar

Related Questions

I need to compare values from two tables that are identical but contain some
I have a HashMap relating Keys to Strings, and I need to compare some
I need to compare two datetime variable only for some criteria : Day of
At some point in an algorithm I need to compare the float value of
I need to compare one given value with a retrieved values. I do this
I need to compare current day with some value. Like from front end I
I have some problem to compare values from different array list From this values,
I just wanted to seek some advice on database design with mongoid and rails.
I need to compare two directories, and produce some sort of structured output (text
I'm doing some QA in Netezza and I need to compare the counts from

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.