has_manyアソシエーションに苦労しています。日記をつけています。モデルプレイヤーは以下の通りです。
- ユーザー
- UserFriend
- UserFoodProfile
ユーザーの友達が食べた食べ物を全部手に入れたいです。だから、私は得ることができるようにしたいと思います:current_user.friends.profiles
これまで、にアクセスできるように関連付けを適切に設定しましたcurrent_user.friends
が、過去30日間のすべての友達のエントリも取得できるようにしたいと考えています。
これが私のモデルです
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
UserFriendモデルは、次のように設定されます。
- id
- ユーザーID
- friend_id
- friend_name
user_food_profiles
ユーザーの友達の現在の情報を「エントリ」として取得できるように、友達から接続したいのですが、user_food_profiles
試したすべてがうまくいきませんでした。この関連付けをどのように設定しますか?
やろうとした:
- UserFriend:
has_many :user_food_profiles, :as => 'entries'
- UserFoodProfile:
belongs_to :friend, :foreign_key => 'friend_id'
これを機能させる方法について何かアイデアはありますか?カスタムfinder_sqlを作成したいのですが、これは関連付けで機能すると確信しています。