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?
read_attribute("price")
else
price = read_attribute("price")
for option in self.options
price += option.price
end
end
end
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
Controller]
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,
Chris
--
Posted via http://www.ruby-forum.com/.