【发布时间】:2010-11-22 03:42:14
【问题描述】:
在关注Ryan Bates' railscast 之后,我已经在我的 Ruby on Rails 应用程序中成功地为用户建立了友谊自我参考协会。我可以成功关注、取消关注和查看谁在关注。现在我想引入另一个元素,但我不知道该怎么做。
当访问current_user 已经是friends 的用户/显示页面时...检查Friendships 表是否存在现有友谊的语法是什么。
这是关联的设置方式。
友谊.rb
belongs_to :user
belongs_to :friend, :class_name => "User"
用户.rb
has_many :friendships
has_many :friends, :through => :friendships
has_many :inverse_friendships, :class_name => "Friendship", :foreign_key => "friend_id"
has_many :inverse_friends, :through => :inverse_friendships, :source => :user
def friends_workouts
@friends_workouts ||= Workout.find_all_by_user_id(self.friends.map(&:id), :order => "created_at DESC", :limit => 3)
end
【问题讨论】:
标签: ruby-on-rails ruby associations