views:

44

answers:

2

Hi there, this is my first time posting here, looks like a great site! I'm struggling with a has_many association. I have a diary application. The Model players are as follows:

  • User
  • UserFriend
  • UserFoodProfile

I want to be able to get at all the foods that a user's friends have eaten. So, I want to be able to get: current_user.friends.profiles

I've setup the associations properly so far so that I'm able to access current_user.friends, but now I want to be able to get all the friend's entries as well over the last 30 days.

Here are my models

class User < ActiveRecord::Base

  cattr_reader :per_page
  @@per_page = 20

  has_many  :user_food_profiles
  has_many  :preferred_profiles
  has_many  :food_profiles, :through => :user_food_profiles
  has_many  :weight_entries
  has_one   :notification
  has_many  :user_friends
  has_many  :friendships, :class_name => "UserFriend", :foreign_key => "friend_id"
  has_many  :friends, :through => :user_friends

class UserFriend < ActiveRecord::Base

  belongs_to :user
  belongs_to :friend, :class_name => "User", :foreign_key => "friend_id"

class UserFoodProfile < ActiveRecord::Base
  belongs_to :user
  belongs_to :food_profile
  belongs_to :post

The UserFriend model is setup the following way:

  • id
  • user_id
  • friend_id
  • friend_name

I want to connect to user_food_profiles from friend so that I can get a user's friend's current user_food_profiles as "entries" but everything I've tried hasn't worked. How would I setup this association?

Tried to do: - UserFriend - has_many :user_food_profiles, :as => 'entries' - UserFoodProfile - belongs_to :friend, :foreign_key => 'friend_id'

Any ideas on how to make this work? Tempted to create a custom finder_sql but I'm sure this can work with associations. Thanks for looking this over!

A: 

Isn't a "friend" just another user that's in the database?

Let your UserFriend be a many_to_many relationship (either with "has_and_belongs_to_many" or "has_many :through"): each user can have several users as friends. You can then link those user_ids (which could be in the many_to_many table called 'friend_id' if you like) to their foodprofile without a problem, since it is using the same link as user.foodprofile .

Trevoke
A: 

This is the line I see being the problem:

class User < ActiveRecord::Base
  # <snip/>
  has_many  :friendships, 
              :class_name => "UserFriend", 
              :foreign_key => "friend_id"

I'm assuming that you're using a join table here called user_friend. That would mean that the foreign key there should be "user_id".

Now, unless you're going to store extra metadata in that UserFriend model, it's not required — you can get away with a self-referential has_and_belongs_to_many relationship like so:

has_and_belongs_to_many :friends, 
                          :class_name => "User", 
                          :join_table => "user_friends", 
                          :foreign_key => "user_id", 
                          :association_foreign_key => "friend_id"

Doing this, all you have to do is user.friends.profiles quite easily.

Now, if the relationship is bi-directional it gets a bit more complex, but I feel like this should at least get you started along the way.

Andrew Harvey