# Structuring Active Record relationships

**URL:** <https://www.ruby-forum.com/t/structuring-active-record-relationships/188442>\
**Category:** Rails\
**Created:** [May 12, 2010, 2:58am UTC](https://www.ruby-forum.com/t/structuring-active-record-relationships/188442 "2010-05-12T02:58:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [May 12, 2010, 2:58am UTC](https://www.ruby-forum.com/t/structuring-active-record-relationships/188442/1 "2010-05-12T02:58:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [May 12, 2010, 3:23am UTC](https://www.ruby-forum.com/t/structuring-active-record-relationships/188442/2 "2010-05-12T03:23:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [May 12, 2010, 10:00am UTC](https://www.ruby-forum.com/t/structuring-active-record-relationships/188442/3 "2010-05-12T10:00:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RichardOnRails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e0b2c6/32.png) [@RichardOnRails](https://www.ruby-forum.com/u/RichardOnRails)\
**Post date:** [May 12, 2010, 5:03pm UTC](https://www.ruby-forum.com/t/structuring-active-record-relationships/188442/4 "2010-05-12T17:03:54Z")

</div>

Hi Sharagoz,

> Does this make sense?

Absolutely! It’s crystal clear.

Many thanks,  
Richard
