# How to create Rails models w/ multiple complex associations?

**URL:** <https://www.ruby-forum.com/t/how-to-create-rails-models-w-multiple-complex-associations/184647>\
**Category:** Rails\
**Created:** [March 5, 2010, 7:58pm UTC](https://www.ruby-forum.com/t/how-to-create-rails-models-w-multiple-complex-associations/184647 "2010-03-05T19:58:57Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dacbo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b9e5f3/32.png) [@dacbo](https://www.ruby-forum.com/u/dacbo)\
**Post date:** [March 5, 2010, 7:58pm UTC](https://www.ruby-forum.com/t/how-to-create-rails-models-w-multiple-complex-associations/184647/1 "2010-03-05T19:58:57Z")

</div>

I am trying to figure out how to create ActiveRecord models with  
associations that can yield the same results as this SQL query:

select login, first\_name, last\_name, email\_address from accounts inner  
join people on person.id = accounts.person\_id inner join  
email\_address\_people on person.id = email\_address\_people.person\_id inner  
join email\_addresses on email\_address.id =  
email\_address\_people.email\_address\_id inner join email\_address\_types on  
email\_address\_types.id = email\_address\_people.email\_address\_type\_id  
where email\_address\_types.email\_address\_type = ‘account’;

The table structure is as follows, and assumes each table has an id per  
normal ActiveRecord convention:

accounts  
id : int  
person\_id : int  
login : string

people  
id : int  
first\_name : string  
last\_name : string

email\_address\_people  
id : int  
person\_id : int  
email\_address\_id : int  
email\_address\_type\_id : int

email\_addresses  
id : int  
email\_address : string

email\_address\_types  
id : int  
email\_address\_type: string

I need the models to be fully functional, and not limited by things like  
:find\_by\_sql.

How do I create the associated models that make this possible?

Thanks!  
Chris B.  
removed\_email\_address@domain.invalid
