Association issues

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.

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?

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

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