Hello all, I have a table that has all existent taxes in my country and I want to use it to calculate the tax value for every product in my Product table. What's the best way to do this? Maybe add some kind of virtual column to Product table? I have the following classes: class Product < ActiveRecord:Base belongs_to: tax end class Tax < ActiveRecord:Base end Can anyone tell me how to do this? Thanks, H
on 2006-03-28 21:57
on 2006-03-28 22:11
On 3/28/06, Hugo <email@example.com> wrote: > belongs_to: tax > end > > class Tax < ActiveRecord:Base > end > > Can anyone tell me how to do this? > > Thanks, > H > Do items in your "taxes" table have product_ids, or do products have a tax_id? In this example, I'll assume that it's the latter. class Product < ActiveRecord::Base belongs_to :tax def price_after_taxes self.price + self.tax.value end end Does that help? You can define methods on your models that refer to associated model classes. This keeps your client code from needing to constantly include/manipulate the associations. Avoiding that where possible makes it easier to modify your design later.