我正在努力與has_many關聯。我有一個日記應用程序。該模型的球員如下: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
- USER_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,但我相信這可以與關聯一起工作。