Forum: Ruby on Rails Tables and Join

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Shandy N. (Guest)
on 2009-03-13 18:55
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
Shandy N. (Guest)
on 2009-03-13 19:36
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.
Rob B. (Guest)
on 2009-03-13 21:05
(Received via mailing list)
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
Frederick C. (Guest)
on 2009-03-14 14:57
(Received via mailing list)
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
This topic is locked and can not be replied to.