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
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.