【问题标题】:How would you represent this logic in models and migration您将如何在模型和迁移中表示此逻辑
【发布时间】:2016-04-22 09:53:16
【问题描述】:

这可能已经被问过了,但我什至不知道如何表达这个适合查询。

我希望调用user1.friends(user1 是 User 模型的一个实例),它会返回一个用户列表,其中包含 user1 作为他们的朋友之一。

我什至不知道从哪里开始。

friends 表的列是什么?还应该有朋友桌吗?

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    也许你可以使用这个RailsCasts

    模型/用户.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
    

    models/friendship.rb

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-22
      • 2010-10-28
      • 1970-01-01
      • 2012-03-14
      • 1970-01-01
      相关资源
      最近更新 更多