Forum: Ruby on Rails Rewriting attribute accessors?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
3c4067229e39ae56a0735b6491020533?d=identicon&s=25 Chris Cole (chriscole)
on 2006-02-22 17:30
I'm new to Rails and so was wondering if the following strategy is a
good idea.

Background: I'm writing a eCommerce application which has a Product
model and an Option model, they are setup with a has_and_belongs_to_many
relationship. The Product model features the 'base' attributes of the
product and then the shopkeeper *can* add one or more Options that
modify those base attributes in some way, i.e. increase/decrease price,
add weight, alter shipping delay etc.

The question: In 'Agile Web Development with Rails' (Ch.15; pg. 284)
Dave & David talk about writing custom accessor methods (under the
banner of Facade Columns, that is, adapting for legacy schemas) - now I
was wondering could I use a similar method to do something like this: -
(excuse if the code is not 100%, but hopefully you'll get the idea)

class Product < ActiveRecord::Base

def price
  if self.options.nil?
    price = read_attribute("price")
    for option in self.options
      price += option.price

The idea being I will still be able to call @product.price etc and the
model will take care of providing the controller with the correctly
adjusted price (and all the other adjusted attributes). [My limited
experience with MVC suggests this is a Model type of logic as opposed to

Am I on completely the wrong track with this, or is this a good idea, or
is there a better way to achieve the smae kind of thing. What are the
pros and cons of such an approach?

Any thoughts very gratefully receieved,
with thanks in advance,
This topic is locked and can not be replied to.