【问题标题】:Friendship self referential association友谊自我参照协会
【发布时间】:2017-10-14 15:18:27
【问题描述】:

我一直在尝试实现 Facebook 风格的好友系统。我已经阅读了几个教程并且无法理解我做错了什么。友谊控制器和视图中用于添加朋友的链接有效,但在数据库级别会引发错误。

当我打开 rails 控制台并放置:

User.find(1).friends << User.find(2)

我明白了:

SQL (0.4ms)  INSERT INTO "friendships" ("user_id", "friend_id", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["user_id", 1], ["friend_id", 2], ["created_at", "2017-10-14 15:01:28.364691"], ["updated_at", "2017-10-14 15:01:28.364691"]]
(0.1ms)  rollback transaction
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.friends: INSERT INTO "friendships" ("user_id", "friend_id", "created_at", "updated_at") VALUES (?, ?, ?, ?)
from (irb):1

用户.rb

has_many :friendships, dependent: :destroy
has_many :friends, through: :friendships

# these are used in the controller

def befriend(user)
  friends << user
end

def unfriend(user)
  friends.delete(user)
end

def friends?(user)
  friends.include?(user)
end

友谊.rb

belongs_to :user
belongs_to :friend, class_name: 'User', foreign_key: "friend_id"

validates :user_id, presence: true
validates :friend_id, presence: true

我正在使用 Devise 进行身份验证。

【问题讨论】:

    标签: ruby-on-rails self-join self-referencing-table


    【解决方案1】:

    您不是在创建朋友(其他用户已经存在),而是友谊

    用户.rb

    class User < ActiveRecord::Base
      has_many :friendships, dependent: :destroy
      has_many :friends, through: :friendships, source: :friend
    end
    

    友谊.rb

    class Friendship < ActiveRecord::Base
      belongs_to :user
      belongs_to :friend, class_name: 'User'
    end
    

    控制台

    User.find(1).friendship.create(friend_id: User.find(2).id)
    

    【讨论】:

    • 您好,不幸的是,您的代码似乎产生了同样的错误。我认为问题在于,当创建友谊时,它会抛出“没有这样的表:main.friends”错误。铲子语法(
    • has_many :through 关联意味着我们可以使用append (&lt;&lt;) 方法来创建友谊。
    • 删除源::朋友类用户,它会工作
    【解决方案2】:

    我创建了两个名为 User 和 Friendship 的表

    > rails g model User first_name:string last_name:string
    > rails g model Friendship user:references friend:references
    > rake db:migrate
    

    user.db中,定义了两个has_many关联:

    class User < ApplicationRecord
        has_many :friendships, dependent: :destroy
        has_many :friends, through: :friendships
    end
    

    friendship.rb 中:

    class Friendship < ApplicationRecord
       belongs_to :user
       belongs_to :friend, class_name: 'User'
    end
    

    在 Rails 控制台中,我创建了两个示例用户并让他们成为朋友:

    > User.create(first_name:"Blue", last_name:"Sky")
    > User.create(first_name:"White", last_name:"Cloud")
    > Friendship.create(user:User.first, friend:User.second)
    > Friendship.create(user:User.second, friend:User.first)
    

    或者:

    > User.first.friendships.create(friend:User.second)
    > User.second.friendships.create(friend:User.first)
    

    现在您可以检索属于一个用户的所有朋友。例如:

    > User.first.friends
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-28
      相关资源
      最近更新 更多