Topic: One model, different tables

Hello,

this may be somewhat unconventional, but I just want to know if it is possible anyway.

So here is the deal.
There will be a database with different tables concerning products.
My product has to be like:

Product(Itemkey, Description, Class, Currency, Price)

So my product model will be like

Itemkey -> database.products.id
Description -> database.products.descr
Class -> database.products.class

Currency -> database.productfinance.currency
Price -> database.productfinane.currency

So you see that I get my data for this product from different tables. I know I could do something like a has_one association, but I don't want to do like 'product.finance.price'. I would rather like product.price without having different models for it.


Greets!

Re: One model, different tables

You could just define the needed instance methods in your Product model:

class Product < AR

#your relations come here, e.g.
has_one :finance

def price
  finance.price
end

end

The same for any other relations methods.
HTH

Last edited by Javix (2012-11-30 06:25:49)

Re: One model, different tables

Thank you!