Has_many and belongs_to question

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 :users
end

class User< ActiveRecord::Base
has_many :users_emails
end


What I’m trying to do is find all the users whom have a particular
email.
I can do it has the following:

  1. get all user_id’s from user_emails table who has email = :email (user
    form input)
  2. then for those user_ids get the user ojects from the users table by
    :find(user_id)

I can get the email’s based on user name by the following:
User.find(1).user_emails but this is not what I want. I want
Users whom have a particular email address.

I’m wondering what is the nice way of doing this (the rails way using
methods on ActiveRecord). Or perhaps my assoications and tables are not
correct to facilitate such a query. The following does not work:
emails = UsersEmail.find(:condition =>…)
emails.users

Any help would be greatly appreciated.

Try this:

UserMail.find_by_email(“[email protected]”).collect(&:user)

It returns an array with all users with email “[email protected]”. Do you
want this?

On Aug 22, 5:58 am, Mike J. [email protected]

Hi,
There are quite a few inconsistencies in your code, not sure if they
are typos in the post or actual errors in your code. You should have
class UsersEmail < AR::Base
belongs_to :user # note the singular here!
end

Then you say you have a user_emails table: If your class is
UsersEmail, your table should be users_emails (users is plural). I
think class UserEmail would be more appropriate naming, but that’s up
to you.

David’s code is then most elegant to fetch all users with a particular
e-mail address. To address why your own code is not working:
emails = UsersEmail.find(:conditions => …) will return you an Array
of UsersEmail objects. So if you want to construct an Array of User
objects from that, you need to indeed to apply a .collect or .map to
it:

foundusers = emails.map { |e| e.user } # note the singular user due to
the belongs_to clause

Again, instead of using :conditions, you can use a dynamic finder if
all you need is find on a specific column’s content:
UsersEmail.find_by_email("…")

Dirk.

It’s a example of ruby sugar code ^^

“UserMail.find_by_email(“[email protected]”).collect(&:user)”
and
“UserMail.find_by_email(“[email protected]”).collect { |var| var.user }”

is the same code. I think it’s useful and it haves some other uses.

Hi,
Thanks for the quick respones. David’s answer works great.
I’m still new to Rails, I was wondering what “:&user” does? I know the
collect method applies “:&user” to all the elements in the hash.

Thanks again.

deegee wrote:

Hi,
There are quite a few inconsistencies in your code, not sure if they
are typos in the post or actual errors in your code. You should have
class UsersEmail < AR::Base
belongs_to :user # note the singular here!
end

Then you say you have a user_emails table: If your class is
UsersEmail, your table should be users_emails (users is plural). I
think class UserEmail would be more appropriate naming, but that’s up
to you.

David’s code is then most elegant to fetch all users with a particular
e-mail address. To address why your own code is not working:
emails = UsersEmail.find(:conditions => …) will return you an Array
of UsersEmail objects. So if you want to construct an Array of User
objects from that, you need to indeed to apply a .collect or .map to
it:

foundusers = emails.map { |e| e.user } # note the singular user due to
the belongs_to clause

Again, instead of using :conditions, you can use a dynamic finder if
all you need is find on a specific column’s content:
UsersEmail.find_by_email("…")

Dirk.

On 22 Aug 2008, at 13:15, Mike J. wrote:

Hi,
Thanks for the quick respones. David’s answer works great.
I’m still new to Rails, I was wondering what “:&user” does? I know
the
collect method applies “:&user” to all the elements in the hash.

it’s &:user.
when you prefix the last argument to a function with & that tells ruby
'this is something like a proc that you should should use as the block
for this function).
I said like a proc, because ruby doesn’t really care. As long as it
has a to_proc method which returns something close enough to a proc
then you’re ok.
rails adds a to_proc method to symbols that generates an appropriate
proc (from 1.8.7/1.9 this is part of the standard library (not 100%
sure about 1.8.7)

Fred

Oh I see.

Thanks!

david wrote:

It’s a example of ruby sugar code ^^

“UserMail.find_by_email(“[email protected]”).collect(&:user)”
and
“UserMail.find_by_email(“[email protected]”).collect { |var| var.user }”

is the same code. I think it’s useful and it haves some other uses.