在 Rails 中创建友谊模型协会

发布于 2024-12-22 21:40:53 字数 1694 浏览 0 评论 0原文

问题:@user.friends 不起作用。它返回 2 条记录,应该是 4 条。

我有以下模型:

class User < ActiveRecord::Base
  has_many :friendships
  has_many :friends, 
    :through => :friendships,
    :conditions => "status = 'accepted'", 
    :order => :fname
  has_many :requested_friends, 
    :through => :friendships, 
    :source => :friend,
    :conditions => "status = 'requested'"
  has_many :pending_friends, 
    :through => :friendships, 
    :source => :friend,
    :conditions => "status = 'pending'"

class Friendship < ActiveRecord::Base
  belongs_to :user
  belongs_to :friend, :class_name => "User", :foreign_key => "friend_id"

出于某种原因。 <%[电子邮件受保护]%> 没有返回用户的所有好友。

示例数据:

> @user.friendships.all.length
=> 4
> @user.friendships
=> [#<Friendship id: 20, user_id: 11, friend_id: 20, status: "accepted", created_at: "2011-12-22 12:59:22", updated_at: "2011-12-22 17:02:54">, #<Friendship id: 8, user_id: 11, friend_id: 12, status: "accepted", created_at: "2011-12-22 06:29:02", updated_at: "2011-12-22 07:41:24">, #<Friendship id: 3, user_id: 11, friend_id: 1, status: "approved", created_at: "2011-12-22 05:48:29", updated_at: "2011-12-22 06:22:09">, #<Friendship id: 1, user_id: 11, friend_id: 641, status: "approved", created_at: "2011-12-22 04:47:19", updated_at: "2011-12-22 04:47:19">]
> @user.friends.length
=> 2

@user.friends.length 应为 4,因为上面的数据显示所有状态均为“已接受”。知道我在上面列出的模型关联中搞砸了什么吗?

谢谢你!

PROBLEM: @user.friends does not work. It's returning 2 records and it should be 4..

I have the following models:

class User < ActiveRecord::Base
  has_many :friendships
  has_many :friends, 
    :through => :friendships,
    :conditions => "status = 'accepted'", 
    :order => :fname
  has_many :requested_friends, 
    :through => :friendships, 
    :source => :friend,
    :conditions => "status = 'requested'"
  has_many :pending_friends, 
    :through => :friendships, 
    :source => :friend,
    :conditions => "status = 'pending'"

class Friendship < ActiveRecord::Base
  belongs_to :user
  belongs_to :friend, :class_name => "User", :foreign_key => "friend_id"

For some reason. <%[email protected]%> is not returning all of the user's friends.

Example data:

> @user.friendships.all.length
=> 4
> @user.friendships
=> [#<Friendship id: 20, user_id: 11, friend_id: 20, status: "accepted", created_at: "2011-12-22 12:59:22", updated_at: "2011-12-22 17:02:54">, #<Friendship id: 8, user_id: 11, friend_id: 12, status: "accepted", created_at: "2011-12-22 06:29:02", updated_at: "2011-12-22 07:41:24">, #<Friendship id: 3, user_id: 11, friend_id: 1, status: "approved", created_at: "2011-12-22 05:48:29", updated_at: "2011-12-22 06:22:09">, #<Friendship id: 1, user_id: 11, friend_id: 641, status: "approved", created_at: "2011-12-22 04:47:19", updated_at: "2011-12-22 04:47:19">]
> @user.friends.length
=> 2

@user.friends.length should have been 4 as the data above shows all the statuses as "accepted". Any idea where I messed up in the model associations listed above?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦中楼上月下 2024-12-29 21:40:53

其中两个好友的状态为“已批准”,两个好友的状态为“已接受”。这就是为什么它根据您的条件仅返回两个接受友谊。

status is "approved" for two of the friendships and "accepted" for two. That's why it is returning only two accepted friendships as per your condition.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文