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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:40:38+00:00 2026-06-06T18:40:38+00:00

In Rails, how do you assign all attributes of an item automatically depending on

  • 0

In Rails, how do you assign all attributes of an item automatically depending on the first attribute

First part of my question is at the link above,

I have grabbed a random property using the example @ring.display_name = Ring::DISPLAY_NAME.sample. That’s not a problem. What is troubling me is grabbing the rest of the attributes.

I now need to grab @ring.gold, @ring.roll and @ring.bonus in accordance to what was randomly selected for the display_name. Here is a look at my model right now, I know it’s wrong, but can you tell me if there is a better way of doing this, one that works perhaps?

class Ring < ActiveRecord::Base



  # Display_name
  DISPLAY_NAME = [ 'Beginners', 'Silver', 'Gold', 'Diamond' ]

  # Gold
  if Ring.display_name == 'Beginners'
    GOLD = [ 0 ]
  end
  if Ring.display_name == 'Silver'
    GOLD = [ 0 ]
  end
  if Ring.display_name == 'Gold'
    GOLD = [ 0 ]
  end
  if Ring.display_name == 'Diamond'
    GOLD = [ 0 ]
  end


  # Roll
  if @ring.display_name == 'Beginners'
    ROLL = [ 1 ]
  end  
  if @ring.display_name == 'Silver'
    ROLL = [ 1, 2, 3 ]
  end
  if @ring.display_name == 'Gold'
    ROLL = [ 2, 3, 4 ]
  end
  if @ring.display_name == 'Diamond'
    ROLL = [ 4, 5, 6 ]
  end

  # Bonus
  if Ring.display_name == 'Beginners'
    BONUS = [ ring.user.level + 1 ]
  end  
  if Ring.display_name == 'Silver'
    BONUS = [ ring.user.level + (1 + rand(3)) ]
  end
  if Ring.display_name == 'Gold'
    BONUS = [ ring.user.level + (1 + rand(5)) ]
  end
  if Ring.display_name == 'Diamond'
    BONUS = [ ring.user.level + (1 + rand(8)) ]
  end

  attr_accessible :description, :display_name, :roll, :bonus, :total, :image, :gold


end

As you can see by the code, I have tried using an ‘if Ring.display_name’ and also a ‘if @ring.display_name’ Neither of these worked, as I assume whoever reading this already knows they woudn’t.

Thanks!

UPDATED!

Here is the code I am currently using. This isn’t right, I don’t quite understand how to use this technique. Take a look at what I have.

class Ring < ActiveRecord::Base

  # Display_name
  DISPLAY_NAME = [ 'Silver', 'Gold', 'Diamond' ]

  # Rolls
  ROLL = {
    'Silver'    => [1, 2, 3],
    'Gold'      => [2, 3, 4],
    'Diamond'   => [4, 5, 6]
  }
  self.roll = ROLL[self.display_name]

  BUY = {
    'Silver'    => [100..180],
    'Gold'      => [140..200],
    'Diamond'   => [180..350]
  }
  self.buy = BUY[self.display_name]

  BONUS = {
    'Silver'    => [(1 + rand(3))],
    'Gold'      => [(3 + rand(5))],
    'Diamond'   => [(5 + rand(8))]
  }
  self.bonus = BONUS[self.display_name].call

  attr_accessible :user, :active, :display_name, :description, :gold, :buy, :sell, :roll, :bonus, :total, :image, :description


end

and in my controller:

def create
    @ring = Ring.new
    @ring.display_name = Ring::DISPLAY_NAME.sample

    @ring.save

    redirect_to @ring
  end

The way I understand it, if I tell it to pick a random display_name, all other fields will be filled in according to what I have in the model, is that correct?

  • 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-06T18:40:38+00:00Added an answer on June 6, 2026 at 6:40 pm

    You can make hashes out of your conditionals, like following:

    class Ring < ActiveRecord::Base
    
      # Display_name
      DISPLAY_NAME = [ 'Silver', 'Gold', 'Diamond' ]
    
      # Rolls
      ROLL = {
        'Silver'    => [1, 2, 3],
        'Gold'      => [2, 3, 4],
        'Diamond'   => [4, 5, 6]
      }
    
      BUY = {
        'Silver'    => [100..180],
        'Gold'      => [140..200],
        'Diamond'   => [180..350]
      }
    
      BONUS = {
        'Silver'    => ->(){(1 + rand(3))},
        'Gold'      => ->(){(3 + rand(5))},
        'Diamond'   => ->(){(5 + rand(8))}
      }
    
      attr_accessible :user, :active, :display_name, :description, :gold, :buy, :sell, :roll, :bonus, :total, :image, :description
    
      def display_name=(name)
        super(name)
        self.roll = ROLL[self.display_name]
        self.buy = BUY[self.display_name]
        self.bonus = BONUS[self.display_name].call
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about handling my models. I get all confused. When I
I am beginning Rails programming and all I want to accomplish is to have
I am trying to assign a form-inline class to the following form in rails
Rails has a nice idiom that makes it easy for you to have a
Rails question. The default behavior for error validation is to fieldWithError-styled div around the
I have a normal Rails project (without Active Record) using CouchDB (couchrest_model) as a
I have written an application for an online clothing store in Rails 2.3.5. I
I have two entities, projects and users. These are modeled in Rails using Mongoid
Is there a way to have rails raise an error if an attempt is
I'm new to Rails. I'm using Service layer to keep my controllers thin. All

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.