我正在尝试在我的Rails 4项目中创建一个“协作”表,但遇到了问题。 我希望它属于单个用户,也就是协作者。
我运行了以下命令来生成模型和迁移,我也将其复制如下。
``` rails generate model Collaboration collaborator:references project:references ```rails generate model Collaboration project:references collaborator:references accepted:boolean
迁移:
class CreateCollaborations < ActiveRecord::Migration
def change
create_table :collaborations do |t|
t.references :project, index: true, foreign_key: true
t.references :collaborator, index: true, foreign_key: true
t.boolean :accepted
t.timestamps null: false
end
end
end
模型:
class Collaboration < ActiveRecord::Base
belongs_to :project
belongs_to :collaborator, class_name: 'User'
end
我更新了协作模型,包括 , class_name: 'User'
如上所示。同样地,我更新了现有的 Strategy
模型,包括一个 has_many :collaborations
。
class Project < ActiveRecord::Base
has_many :collaborations
end
当我运行
rake db:migrate
时,会报告以下错误。rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR: relation "collaborators" does not exist
我有点困惑为什么会发生这种情况。非常感谢您的帮助!谢谢。:)
编辑:
同时添加我的User
模型代码。
class User < ActiveRecord::Base
authenticates_with_sorcery!
has_many :projects
has_many :collaborations
end
我删除了密码、电子邮件等字段的验证,以尝试去除杂乱。
class_name:
别名,但是您的协作仅使用一次用户。我真的认为现在应该尝试使用belongs_to:user
,即使只是出于诊断目的。 我所知道的是,您正在做的事情很好,我从未在野外遇到过它。 - MarsAtomic