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

How can I set default value in ActiveRecord? I see a post from Pratik
I have a model: class Item < ActiveRecord::Base Item has a property 'store' Based
I have many items and many users in a database. A user can set
I have a table called items. I want to change the description attribute (it's
I have a module: module Voteable def has_up_vote_of user return ! self.votes.select{|v| v.user.id ==
I was implementing my first HABTM relationship and have run into an issue with
I have a users model and a book model. Users can read books (as
Basically whats happening is I can create a new item that gets saved to
Hey, I am writing a website similar to 4chan. I have two models: Board
What's the best way to update table values based on some given condition in

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.