Forum: Ruby on Rails How to create a taxes column

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Hugo (Guest)
on 2006-03-28 21:57
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
Wilson B. (Guest)
on 2006-03-28 22:11
(Received via mailing list)
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.
This topic is locked and can not be replied to.