Structuring Active Record relationships

Hi,

I have three tables: Expenses, Vendors and Vendor _Accounts

It should operate as follows:
When a new Expense record is created,
A vendor is selected from a drop-down of Vendors and,
An account is selected from a drop-down of Vendor_Accounts where
Vendor_Accounts#vender == selected vendor

So I should have a structure something like:
class Expense
has_one Vendor

class Vendor
has_many Vendor_Accounts where Vendor#id == Vendor_Accounts#vendor
belongs_to Expense

class Vendor_Account
belongs_to Vendor

I don’t think I should have Expense, Vendor and Account tables because
the accounts defined by each vendor bear no relationship with the
other vendors’ account beyond the mere concept of “account”.

Does this make sense, and if so, how precisely should I describe
these relationships? Am I close with the above idea?

BTW, I’m running Rails 2.3.5, Ruby 1.8.6, WinXP-Pro/SP3, Firefox
3.6.2, Firebug 1.5.3,
MySQL 5.0.37-community-nt, Mongrel, Apache HTTP Server 2.2.15

Thanks in Advance,
Richard

Thinking about it some more, I now think I should have separate
Expense, Vendor and Account tables with the following relationships:

class Expense
has_one Vendor

class Vendor
has_many Accounts
belongs_to Expense

class Account
belongs_to Vendor

Then an Expense record can get Accounts through Vendor. … or
something like that.

Am I getting warmer?

Thanks in Advance,
Richard

On May 11, 8:57 pm, RichardOnRails

Expense would belong to Vendor, not have one Vendor, unless you are
planning to put the foreign key in the vendors table, which sounds
very strange, as that would require a new vendor to be created for
every expense.
If you are fist selecting a vendor, and then selecting an account
belonging to that vendor when you create an expense, then it makes
sense to hook expense up to account, not vendor.

class Vendor
has_many :accounts

class Account
belongs_to :vendor
has_many :expenses

class Expense
belongs_to :account

Then when you have created an expense you can get its account through
@expense.account and its vendor through @expense.account.vendor.
Does this make sense?

On May 12, 3:22 am, RichardOnRails

Hi Sharagoz,

Does this make sense?

Absolutely! It’s crystal clear.

Many thanks,
Richard