# Tables and Join

**URL:** <https://www.ruby-forum.com/t/tables-and-join/162780>\
**Category:** Rails\
**Created:** [March 13, 2009, 5:55pm UTC](https://www.ruby-forum.com/t/tables-and-join/162780 "2009-03-13T17:55:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [March 13, 2009, 5:55pm UTC](https://www.ruby-forum.com/t/tables-and-join/162780/1 "2009-03-13T17:55:31Z")

</div>

I have two tables - Account and Enpseudo. My accounts table has an  
enpseudo\_id. I am trying to do an inner join on Enpseudo and keep  
getting this error:

Association named ‘account’ was not found; perhaps you misspelled it?

I imagine this means that I have the associations in my models sut-up  
wrong.

In my Account model I have: belongs\_to :enpseudo

In my Enpseudo model I have: has\_many :accounts

Here is the code to my find:

@accountsfrompseudo = Enpseudo.find( :all, :joins =\> :account,  
:conditions =\> { :accounts =\> { :enpseudo\_id =\> userpseudo.enpseudo\_id  
}})

userpseudo.enpseudo\_id is a varible that I am getting from yet a third  
table that is just containing an enpseudo id.

My question is do I have my models set-up correctly? If someone could  
point me to some references on rails and table set-up and associations,  
that would be great too. Thanks,

-S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [March 13, 2009, 6:36pm UTC](https://www.ruby-forum.com/t/tables-and-join/162780/2 "2009-03-13T18:36:59Z")

</div>

I tried enpseudo.accounts to make sure that association was working and  
it was. I ended up doing straight sql to get what I needed, but I  
thought learning how to do it the rails way would be interesting.

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [March 14, 2009, 1:57pm UTC](https://www.ruby-forum.com/t/tables-and-join/162780/3 "2009-03-14T13:57:12Z")

</div>

On Mar 13, 4:55 pm, Shandy N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> In my Enpseudo model I have: has\_many :accounts
> 
> Here is the code to my find:
> 
> @accountsfrompseudo = Enpseudo.find( :all, :joins =\> :account,

What you pass to :joins must exactly match the name of the association  
(the first argument to has\_many, belongs\_to etc…).  
So if you have has\_many :accounts then you need :joins =\> :accounts or  
else you will get that error message (which is normal: you don’t have  
an association named account only one named accounts).

Fred

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [March 13, 2009, 8:05pm UTC](https://www.ruby-forum.com/t/tables-and-join/162780/4 "2009-03-13T20:05:16Z")

</div>

On Mar 13, 2009, at 12:55 PM, Shandy N. wrote:

> table that is just containing an enpseudo id.
> 
> My question is do I have my models set-up correctly? If someone could  
> point me to some references on rails and table set-up and  
> associations,  
> that would be great too. Thanks,
> 
> ## -S

You almost have it.

@accountsfrompseudo = Enpseudo.find(userpseudo.enpseudo\_id,  
:include =\> :accounts)

Or if your “third table” (assuming Userpseudo model)

class Userpseudo  
belongs\_to :enpseudo  
end

Then you get the same result with:

@accounts\_from\_pseudo = userpseudo.user.accounts

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
