# has\_many and belongs\_to question

**URL:** <https://discuss.rubyonrails.org/t/has-many-and-belongs-to-question/33484>\
**Category:** rubyonrails-talk\
**Created:** [August 22, 2008, 3:58am UTC](https://discuss.rubyonrails.org/t/has-many-and-belongs-to-question/33484 "2008-08-22T03:58:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 22, 2008, 3:58am UTC](https://discuss.rubyonrails.org/t/has-many-and-belongs-to-question/33484/1 "2008-08-22T03:58:09Z")

</div>

Hi, &nbsp;&nbsp;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 &nbsp;&nbsp;belongs\_to :users end

class User\< ActiveRecord::Base &nbsp;&nbsp;has\_many :users\_emails end

---

<div class="post-metadata">

**Author:** ![david4](https://avatars.discourse-cdn.com/v4/letter/d/8e7dd6/32.png) [@david4](https://discuss.rubyonrails.org/u/david4)\
**Post date:** [August 22, 2008, 6:07am UTC](https://discuss.rubyonrails.org/t/has-many-and-belongs-to-question/33484/2 "2008-08-22T06:07:33Z")

</div>

Try this:

UserMail.find\_by\_email("foo@bar.com").collect(&:user)

It returns an array with all users with email "foo@bar.com". Do you want this?

---

<div class="post-metadata">

**Author:** ![Dirk\_Groten](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dirk\_Groten](https://discuss.rubyonrails.org/u/Dirk_Groten)\
**Post date:** [August 22, 2008, 10:21am UTC](https://discuss.rubyonrails.org/t/has-many-and-belongs-to-question/33484/3 "2008-08-22T10:21:23Z")

</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 &nbsp;&nbsp;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:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [August 22, 2008, 12:15pm UTC](https://discuss.rubyonrails.org/t/has-many-and-belongs-to-question/33484/4 "2008-08-22T12:15:55Z")

</div>

Hi, &nbsp;&nbsp;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:

---

<div class="post-metadata">

**Author:** ![david4](https://avatars.discourse-cdn.com/v4/letter/d/8e7dd6/32.png) [@david4](https://discuss.rubyonrails.org/u/david4)\
**Post date:** [August 22, 2008, 12:20pm UTC](https://discuss.rubyonrails.org/t/has-many-and-belongs-to-question/33484/5 "2008-08-22T12:20:46Z")

</div>

It's a example of ruby sugar code ^^

"UserMail.find\_by\_email("f...@bar.com").collect(&:user)" and "UserMail.find\_by\_email("foo@bar.com").collect { |var| var.user }"

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

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [August 22, 2008, 12:23pm UTC](https://discuss.rubyonrails.org/t/has-many-and-belongs-to-question/33484/6 "2008-08-22T12:23:20Z")

</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.

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
