# AR association question

**URL:** <https://www.ruby-forum.com/t/ar-association-question/134894>\
**Category:** Rails\
**Created:** [April 16, 2008, 12:04am UTC](https://www.ruby-forum.com/t/ar-association-question/134894 "2008-04-16T00:04:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [April 16, 2008, 12:04am UTC](https://www.ruby-forum.com/t/ar-association-question/134894/1 "2008-04-16T00:04:42Z")

</div>

Hi there,

I have a favorites table in my DB that has a user\_id and restaurant\_id  
columns.

In my restaurant controller I have a user ID and need to get all the  
restaurants that the user has marked as favorites. I would like to do  
something similiar to:

@results = Favorite.find\_all\_by\_user\_id(current\_user.id)

This however returns a list of “favorites” not “restaurants”. What is  
the best way to get the list of restaurants?

Thanks,  
steve

---

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [April 16, 2008, 12:57am UTC](https://www.ruby-forum.com/t/ar-association-question/134894/2 "2008-04-16T00:57:59Z")

</div>

Or if you don’t:

@results =  
Restaurant.find\_all\_by\_id(Favorite.find\_all\_by\_user\_id(current\_user.id).map  
{|x| x.restaurant\_id})

On Apr 15, 3:47 pm, “removed\_email\_address@domain.invalid” \<ESPN3.DL-

---

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [April 16, 2008, 12:47am UTC](https://www.ruby-forum.com/t/ar-association-question/134894/3 "2008-04-16T00:47:28Z")

</div>

@results = User.find(current\_user.id).restaurants

That will work if you have the habtm relationships set up correctly

On Apr 15, 3:04 pm, Steve G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [April 16, 2008, 4:27pm UTC](https://www.ruby-forum.com/t/ar-association-question/134894/4 "2008-04-16T16:27:51Z")

</div>

On 16 Apr 2008, at 15:16, Steve G. wrote:

> Thanks for the help. The first suggestion didn’t work - the second one  
> did however. I have the habtm relationships set up as follows:

You don’t have any hatbm relation ships.

Add has\_many :restaurants, :through =\> :favourites to User and you  
will be able to do things like User.find(…).restaurants.

Fred

---

<div class="post-metadata">

**Author:** ![sjglazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ee59a6/32.png) [@sjglazer](https://www.ruby-forum.com/u/sjglazer)\
**Post date:** [April 16, 2008, 4:16pm UTC](https://www.ruby-forum.com/t/ar-association-question/134894/5 "2008-04-16T16:16:08Z")

</div>

Thanks for the help. The first suggestion didn’t work - the second one  
did however. I have the habtm relationships set up as follows:

class Favorite \< ActiveRecord::Base  
belongs\_to :restaurant  
belongs\_to :user

class User \< ActiveRecord::Base  
has\_many :favorites, :dependent =\> :destroy

class Restaurant \< ActiveRecord::Base  
has\_many :favorites , :dependent =\> :destroy

Any ideas why the first solution doesn’t work?(it says there is not such  
method “Restaurants”)

Thanks,  
steve

removed\_email\_address@domain.invalid wrote:

> @results = User.find(current\_user.id).restaurants
> 
> That will work if you have the habtm relationships set up correctly
> 
> On Apr 15, 3:04ï¿½pm, Steve G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
