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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:06:11+00:00 2026-06-04T19:06:11+00:00

I have a question about models on Ruby on Rails: I have this method

  • 0

I have a question about models on Ruby on Rails:

I have this method on Rails that receives a name of the column and the new value for that column:

def change_attribute
    field = params[:field]
    new_value = params[:new_value]
    person = Person.find(params[:id])
    person.update_attribute(field, new_value)
    render :text=>"", :layout=>false
end

But this throws me an error: that field doesn’t exist, I think it should be something like :name, :lastname, etc.

How can I convert my field variable into one of them dinamically to make this function work?

  • 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-04T19:06:12+00:00Added an answer on June 4, 2026 at 7:06 pm

    You should be checking whether the field name you get in params[:field] is exactly the same as the original field name (no extra spaces etc.). You can check it by puts params[:field].inspect and checking it in the terminal (where the rails server is started).

    According to the docs for update_attribute, we will be able to pass string or symbol as the field name. So, you need not worry about converting the field parameter to symbol before passing it to update_attribute. As you can see the source, here:

    def update_attribute(name, value)
      name = name.to_s
      raise ActiveRecordError, "#{name} is marked as readonly" if self.class.readonly_attributes.include?(name)
      send("#{name}=", value)
      save(:validate => false)
    end
    

    It is first converted to string and then the setter is called. As @jdoe mentioned, it is not validating the model (see the save(:validate => false) line in code), so you should be aware of that when you are using this method.

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

Sidebar

Related Questions

I have a general question about rails controllers/models: I have a model Providers, that
I have a question about MVC. Particularly about models. Suppose that I have a
I have a question about random entries in Rails 3. I have two models:
I have a question about how to set up the relations between two models
I have asked a question on SuperUser about updating Ruby version in Google SketchUp
I have a question about handling my models. I get all confused. When I
I have a question about doing a query through a few associations using Ruby
I'm just start to develop on Ruby on Rails and i've question about complex
I guess this is quite a strange question about models transformation. I am starting
I'm starting out with Backbone.JS, and have a question about how this should work.

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.