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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:08:48+00:00 2026-05-28T08:08:48+00:00

there is something that confuses me a little bit and i would like some

  • 0

there is something that confuses me a little bit and i would like some clarification please, since it causes me some trouble.

I have a city model with a delegate to a wood_production attribute that specifies the amount of wood of that city. It’s like:

has_one :wood_production, :autosave => true
delegate :amount, :to => :wood_production, :prefix => true, :allow_nil => true

    def wood
        wood_production_amount
    end

    def wood= amt
      self[:wood_production_amount] = amt
    end

I normally wanted to be able to do a city.wood -= 1000 and save that value through the city, but i’ve come into all sorts of problems doings this. It seems that i am not setting my virtual attributes correctly maybe.

So i would actually like to ask, what is the difference between these :

def wood
    self.wood_production_amount
end

def wood
    wood_production_amount
end

def wood
    self[:wood_production_amount]
end

and what should really be used to correctly handle the situation ?

EDIT :

If i create the setter like :

def wood= amt
  self.wood_production_amount = amt
end

I get :

1.9.2p290 :003 > c.wood -= 1000
 => 58195.895014789254 
1.9.2p290 :004 > c.save
   (0.1ms)  BEGIN
   (0.3ms)  UPDATE `wood_productions` SET `amount` = 58195.895014789254, `updated_at` = '2012-01-24 02:13:00' WHERE `wood_productions`.`id` = 1
   (2.0ms)  COMMIT
 => true

1.9.2p290 :005 > c.wood
 => 66522.63434300483         ???????

Buf if the setter is :

def wood= amt
  wood_production_amount = amt
end

1.9.2p290 :004 > c.wood -= 1000
 => 58194.823000923556 
1.9.2p290 :005 > c.save
   (0.1ms)  BEGIN
   (0.2ms)  COMMIT
 => true
  • 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-28T08:08:49+00:00Added an answer on May 28, 2026 at 8:08 am

    Answering the first part, self.wood_production_amount and wood_production_amount are functionally identical. The only difference is that in the latter, self is implied, being the current instance of the City model. I rarely use self.anything unless it’s required.

    self[:wood_production_amount] is functionally similar to the first two in most cases. The difference is that it allows you to easily overwrite default accessor methods. read_attribute(:attribute) is functionally identical to self[:attribute]. For example, say your City model has a state attribute, but you want to always return the state in uppercase when it is requested. You could do something like this:

    class City < ActiveRecord::Base
      def state
        self[:state].try(:upcase)
        # or
        read_attribute(:state).try(:upcase)
      end
    end
    
    city = City.new(:state => 'vermont')
    city.state # => VERMONT
    

    So to answer your second question, it really depends on how you want to use it. Personally, I would go with the delegate method unless you need to overwrite some behavior. The reason it wasn’t working for you might be that you aren’t delegating the setter method :amount= as well:

    delegate :amount, :amount= :to => :wood_production, 
             :prefix => true, :allow_nil => true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

cgi.escape seems like one possible choice. Does it work well? Is there something that
I am trying some things with JSON and PHP and there is something that
I am playing a bit with fork/vfork functions, and there is something that is
I am a little confused here. I would like to do something like this:
There is something that confuses me. Let there be a class member Foo::$bar ,
there is something that I miss about the cc-NUMA architecture and the need to
If there's something that everybody hates about Magento it's endlessly configuring your modules before
I am playing with the .net remoting features and there is something that I
math.h is not really a documentation. Is there something else that will describe these
I am using the CodeIgniter DataMapper ORM, but there is something that I don't

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.