Hi,
I have two tables, "users" and "users_emails".
users have the following columns: |id|name|
users_emails have the following columns: |user_id|email|
The classes look like the following:
class UsersEmail< ActiveRecord::Base
belongs_to :user
end
class User< ActiveRecord::Base
has_many :users_emails
end
---
What is the best way (the rails way using ActiveRecord methods) to
find
a user based on "User.name" and "UsersEmail.email". I want the User
who
has a particular name and particular email.
I can do something like:
User.find(:all, :condition["name = :name", 'joe'],
:join => ["do a join on Users and UsersEmail"])
where I specify the user.name in the :condition, but I was wondering
if
there is a better way of doing this (without explicitly witting out
the
join)?
If you do :joins => :users_emails ActiveRecord will write the join
clause for you
But I was wondering if there was a way to avoid the :join condition all
together in the :find. Such as using ":though" in the has_many method
in ActiveRecord?
But I was wondering if there was a way to avoid the :join condition
all
together in the :find. Such as using ":though" in the has_many method
in ActiveRecord?
through is for something different (when you have a many to many
association) which you don't have here.
I'm not sure what your aversion to :joins is when what you're doing is
inherently a joiny sort of thing.
But when I look at the SQL being generated, it looks like a join isn't
taking place. The actual error I get is that it cannot find the
attribute 'email'.
hi,
UsersEmail.find(:all, :conditions => ["email = 'joe@example.com' "])
Returns me an array of all users_emails rows which have email
'joe@example.com'
The problem is I need to also need to do a select based on users.name.
Fred suggested doing a :join using with the syntax ":join =>
:users_emails"
But the following did not work. When I look at the SQL rails tried to
generate, it did not seem to do the join.
User.find(:all, :conditions => ["name = 'joe' and email =
'foo@foo.com"], :joins => :users_emails]
The actual error I get is "ActiveRedocrd::StatementInvalid:
Mysql::Error: #42S22Unknown column 'email' in 'whre clause': SELECT *
FROM users users_emails WHERE users_emails (email = 'joe')
hi,
UsersEmail.find(:all, :conditions => ["email = 'joe@example.com' "])
Returns me an array of all users_emails rows which have email
'joe@example.com'
The problem is I need to also need to do a select based on users.name.
Fred suggested doing a :join using with the syntax ":join =>
:users_emails"
But the following did not work. When I look at the SQL rails tried to
generate, it did not seem to do the join.
User.find(:all, :conditions => ["name = 'joe' and email =
'foo@foo.com"], :joins => :users_emails]
The actual error I get is "ActiveRedocrd::StatementInvalid:
Mysql::Error: #42S22Unknown column 'email' in 'whre clause': SELECT *
FROM users users_emails WHERE users_emails (email = 'joe')
It's very unhelpful when you type error messages in, introducing
errors in the process rather than just copy and pasting them. Be sure
that what you pass to joins is precisely the name of the association
(ie don't pluralize it unless it's a plural association etc)
Hi,
The name of the assoication passed into the join is exactly the same
as the one defined in the model class.
This is the exact error I get, the Join is not being generated in SQL.:
ActiveRecord::StatementInvalid: Mysql::Error: #42S22Unknown column
'email' in 'w
here clause': SELECT * FROM users users_emails WHERE (email ='joe')
Hi,
The name of the assoication passed into the join is exactly the same
as the one defined in the model class.
:joins can work in two ways: association names or SQL fragments. It
looks like rails thinks you mean the latter when you mean the former.
I don't recall precisely what criteria is used but if you are using
strings then make sure you are using symbols