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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:53:20+00:00 2026-05-28T16:53:20+00:00

i am creating a rails application and there is a part that is causing

  • 0

i am creating a rails application and there is a part that is causing me some trouble on the way i designed my code. I am feeling that i’m doing this a bit too messy and i would like to know if there is a design pattern or generally a better way to do this. The situation is like this :

I have a City model and the city can store wood, stone and gold. Those resources are changing over time based on their timestamps. They have their own attributes like amount, changed_timestamp.

Now, i thought that creating 3 separate models like wood_production, stone_production and gold_production seemed like a good idea, seemed pretty adequate. I also created delegators and virtual attributes so that i can execute something like :

city.wood or city.stone

However, the problems arise when i want to change the amount or timestamp for a specific resource type. Say that a building costs

{:wood => 200, :stone => 100, :gold => 50}

to be created. So say that the building is created and i now have to reduce the resources from the city. This would need me to do something like :

self.wood_production.update_attribute(:change_timestamp, Time.now)
self.wood -= v
self.save

The problem with this is that it’s resource specific. I would much more like to have a set_resource method that carries on with the procedure without caring about the resource type (without using ifs and more spaghetti).

How would you do it ?

  • 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-28T16:53:20+00:00Added an answer on May 28, 2026 at 4:53 pm

    The simplest way without changing anything and using the existing code you have is to just add a method like:

    def modify_resource!(type, quantity)
        self.send("#{type}_production").update_attribute(:change_timestamp, Time.now)
        self.send("#{type}=", self.send(type) + quantity)
        self.save
    end
    

    Calling city.modify_resource!(:wood, -10) would subtract 10 wood and update the timestamp.

    It would also depend on how you’re storing the data though. Is the resource total stored with the city, or is it stored in *_production tables and self.wood is just an alias to wood_production?

    There aren’t many other ways of organizing your classes unless you want to go with a simpler method that stores more data per city, such as storing the change_timestamp with the city model rather than in a separate table.

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

Sidebar

Related Questions

I am creating a rails application that needs to store a large amount of
I'm creating a fast application with ruby on rails, and after doing a lot
I am creating a rails app and have used this code in one of
I'm creating a Ruby on Rails application, and I'm trying to create/login/logout users. This
I'm creating a new plugin for a jruby on rails application that will eventually
I am creating a custom CSS stylesheet for a Rails 3 application. Is there
I am creating a Rails application that is a blogging platform, with many contributing
Is there an equivalent to Apache Camel in Rails ?! I'm creating an application
I'm creating a Rails application, and I've hit a bit of a snag. I
We are creating a rails application for an already existing database. We need to

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.