How to create a taxes column


#1

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


#2

On 3/28/06, Hugo removed_email_address@domain.invalid 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.