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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:53:07+00:00 2026-05-19T11:53:07+00:00

I have an item ActiveRecords and I am trying to set a default value

  • 0

I have an item ActiveRecords and I am trying to set a default value (“Test item”) for each of them using a block.
In this expression:

list = {"type1", "type2", "type3", "type4", "..."}
list.each { |name| @item.attributes["#{name}"] = "Test item"] }

values aren’t set.

I must use @item.attributes["#{name}"] for interpolation because I can’t do this for every item:

@item.tipe1 = "Test item"

So, what happens in the first statement? Why? If what I would like to do is not possible in that way, how I can do the same?

  • 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-19T11:53:08+00:00Added an answer on May 19, 2026 at 11:53 am

    The assignment @items.attributes["#{name}"] = "Test item"] does not work, because the attributes method returns a new Hash object each time you call it. So you are not changing the value of the @items‘ object as you thought. Instead you are changing the value of the new Hash that has been returned. And this Hash is lost after each iteration (and of course when the each block has finished).

    A possible solution would be to create a new Hash with the keys of the @items‘ attributes and assign this via the attributes= method.

    h = Hash.new
    
    # this creates a new hash object based on @items.attributes
    # with all values set to "Test Item"
    @items.attributes.each { |key, value| h[key] = "Test Item" }
    
    @items.attributes = h
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an item layout like this, and set the background by using item
I have a trouble trying to get this working. I have an Item model,
I have this item renderer MyRenderer.mxml <?xml version=1.0 encoding=utf-8?> <mx:HBox xmlns:mx=http://www.adobe.com/2006/mxml implements=mx.core.IDataRenderer > <mx:Script>
I am trying to create multiple items (each with a name value and a
I have two models, Item and Link. Each link is going to join two
I have listview item like below <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=match_parent android:layout_height=60dp android:orientation=horizontal android:descendantFocusability=blocksDescendants > <TextView
We have an item cull that retrieves items and their appropriate data from one
I have an item that when hovering in IE7 is causing the item below
I have a treeview (winforms) which have different item types on it. I have
I have that situation: I have models Item, Region and Country. class Item(models.Model): name

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.