# Has\_many and belongs\_to question

**URL:** <https://www.ruby-forum.com/t/has-many-and-belongs-to-question/146540>\
**Category:** Rails\
**Created:** [August 22, 2008, 5:58am UTC](https://www.ruby-forum.com/t/has-many-and-belongs-to-question/146540 "2008-08-22T05:58:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cobra050](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3d9bf3/32.png) [@cobra050](https://www.ruby-forum.com/u/cobra050)\
**Post date:** [August 22, 2008, 5:58am UTC](https://www.ruby-forum.com/t/has-many-and-belongs-to-question/146540/1 "2008-08-22T05:58:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cobra050](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3d9bf3/32.png) [@cobra050](https://www.ruby-forum.com/u/cobra050)\
**Post date:** [August 22, 2008, 8:08am UTC](https://www.ruby-forum.com/t/has-many-and-belongs-to-question/146540/2 "2008-08-22T08:08:12Z")

</div>

Try this:

UserMail.find\_by\_email(“removed\_email\_address@domain.invalid”).collect(&:user)

It returns an array with all users with email “removed\_email\_address@domain.invalid”. Do you  
want this?

On Aug 22, 5:58 am, Mike J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![cobra050](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3d9bf3/32.png) [@cobra050](https://www.ruby-forum.com/u/cobra050)\
**Post date:** [August 22, 2008, 12:22pm UTC](https://www.ruby-forum.com/t/has-many-and-belongs-to-question/146540/3 "2008-08-22T12:22:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cobra050](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3d9bf3/32.png) [@cobra050](https://www.ruby-forum.com/u/cobra050)\
**Post date:** [August 22, 2008, 2:22pm UTC](https://www.ruby-forum.com/t/has-many-and-belongs-to-question/146540/4 "2008-08-22T14:22:09Z")

</div>

It’s a example of ruby sugar code ^^

“UserMail.find\_by\_email(“removed\_email\_address@domain.invalid”).collect(&:user)”  
and  
“UserMail.find\_by\_email(“removed\_email\_address@domain.invalid”).collect { |var| var.user }”

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

---

<div class="post-metadata">

**Author:** ![cobra050](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3d9bf3/32.png) [@cobra050](https://www.ruby-forum.com/u/cobra050)\
**Post date:** [August 22, 2008, 2:15pm UTC](https://www.ruby-forum.com/t/has-many-and-belongs-to-question/146540/5 "2008-08-22T14:15:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cobra050](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3d9bf3/32.png) [@cobra050](https://www.ruby-forum.com/u/cobra050)\
**Post date:** [August 22, 2008, 2:23pm UTC](https://www.ruby-forum.com/t/has-many-and-belongs-to-question/146540/6 "2008-08-22T14:23:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cobra050](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3d9bf3/32.png) [@cobra050](https://www.ruby-forum.com/u/cobra050)\
**Post date:** [August 22, 2008, 2:24pm UTC](https://www.ruby-forum.com/t/has-many-and-belongs-to-question/146540/7 "2008-08-22T14:24:05Z")

</div>

Oh I see.

Thanks!

david wrote:

> It’s a example of ruby sugar code ^^
> 
> “UserMail.find\_by\_email(“removed\_email\_address@domain.invalid”).collect(&:user)”  
> and  
> “UserMail.find\_by\_email(“removed\_email\_address@domain.invalid”).collect { |var| var.user }”
> 
> is the same code. I think it’s useful and it haves some other uses.
