Tables and Join


#1

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


#2

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.


#3

On Mar 13, 4:55 pm, Shandy N. 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


#4

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
removed_email_address@domain.invalid