# Association issues

**URL:** <https://www.ruby-forum.com/t/association-issues/179198>\
**Category:** Rails\
**Created:** [November 19, 2009, 1:51pm UTC](https://www.ruby-forum.com/t/association-issues/179198 "2009-11-19T13:51:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aniltc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c57346/32.png) [@aniltc](https://www.ruby-forum.com/u/aniltc)\
**Post date:** [November 19, 2009, 1:51pm UTC](https://www.ruby-forum.com/t/association-issues/179198/1 "2009-11-19T13:51:00Z")

</div>

hi all

I have list of users and each user has so many friends and each users  
has a list of countries that they have visited and want to visit.

I have created the rails association as

User Model

* * *

class User \< ActiveRecord::Base  
has\_many :friends  
has\_many :visits

end

friend Model

* * *

class Friend \< ActiveRecord::Base  
belongs\_to :user  
end

Country model

* * *

class Country \< ActiveRecord::Base

end

visit Model

* * *

class Visit \< ActiveRecord::Base  
belongs\_to :user  
belongs\_to :country  
end

I am not able to create a rails relation ship.but able to create a raw  
mysql query as

“SELECT Users.ID,Firstname, countries.name ,  
visits.visitstatus FROM users INNER JOIN friends ON users.id =  
friends.friend\_id  
INNER JOIN visits ON users.ID = visits.userid INNER JOIN countries ON  
visits.countryid = countries.id  
WHERE (friends.user\_id = #{@userid}) AND (countries.id  
=’#{@countryid}’)”

How can I achieve this in rails asocoation.

---

<div class="post-metadata">

**Author:** ![aniltc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c57346/32.png) [@aniltc](https://www.ruby-forum.com/u/aniltc)\
**Post date:** [November 19, 2009, 4:53pm UTC](https://www.ruby-forum.com/t/association-issues/179198/2 "2009-11-19T16:53:37Z")

</div>

It’s hard to reverse engineer that big chunk of sql - can you just state  
in english what you want to do? eg, which association(s) are you having  
problems with?

---

<div class="post-metadata">

**Author:** ![aniltc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c57346/32.png) [@aniltc](https://www.ruby-forum.com/u/aniltc)\
**Post date:** [November 19, 2009, 5:00pm UTC](https://www.ruby-forum.com/t/association-issues/179198/3 "2009-11-19T17:00:12Z")

</div>

Max W. wrote:

> It’s hard to reverse engineer that big chunk of sql - can you just state  
> in english what you want to do? eg, which association(s) are you having  
> problems with?

hi

I am looking for Creating a search that allows a user to search their  
friends and matches who have visited or want to visit a specific  
destination

---

<div class="post-metadata">

**Author:** ![aniltc](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c57346/32.png) [@aniltc](https://www.ruby-forum.com/u/aniltc)\
**Post date:** [November 20, 2009, 4:35am UTC](https://www.ruby-forum.com/t/association-issues/179198/4 "2009-11-20T04:35:45Z")

</div>

hi

1. I have a list of users. Each user has a list of countries that they  
have visited and want to visit (in a destinations table. A status of 1  
means they have visited, a 2 means they want to visit the destination)
2. Users have friends and matches that also have lists of countries  
visited/want to visit  
3)I need to Create a search that allows a user to search their friends  
and matches who have visited or want to visit a specific destination.

I am new to rails.

can anyone guide me to create the association. I have create the  
association as  
given below.I am not sure whether it is right or not .

User Model

* * *

class User \< ActiveRecord::Base  
has\_many :friends  
has\_many :visits

end

friend Model

* * *

class Friend \< ActiveRecord::Base  
belongs\_to :user  
end

Country model

* * *

class Country \< ActiveRecord::Base

end

visit Model

* * *

class Visit \< ActiveRecord::Base  
belongs\_to :user  
belongs\_to :country  
end
