Rails 5:添加belongs_to关联

时间:2018-02-26 19:42:48

标签: ruby-on-rails ruby ruby-on-rails-4

我有两张桌子,"用户"和#34;友谊",我试图建立关系,以便用户可以与其他用户建立友谊,因此与rails g model Friendship user:references friend:references status

生成此模型友谊

在模型friendship.rb中:

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

但在创建记录时,它显示以下错误:

Friendship.create(user_id: 1, friend_id:2, id: 1)

   (0.1ms)  begin transaction
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 2], ["LIMIT", 1]]
  SQL (0.4ms)  INSERT INTO "friendships" ("id", "user_id", "friend_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)  [["id", 1], ["user_id", 1], ["friend_id", 2], ["created_at", "2018-02-26 19:39:57.865042"], ["updated_at", "2018-02-26 19:39:57.865042"]]
   (0.1ms)  rollback transaction
Traceback (most recent call last):
        1: from (irb):2
ActiveRecord::StatementInvalid (SQLite3::SQLException: no such table: main.friends: INSERT INTO "friendships" ("id", "user_id", "friend_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?))
irb(main):003:0> 

它可能是什么?我有Rails 5.1.5

0 个答案:

没有答案