【发布时间】:2017-06-26 03:30:30
【问题描述】:
class User < ApplicationRecord
has_many :created_events, :foreign_key => "creator_id", :class_name => "Event"
end
class Event < ApplicationRecord
belongs_to :creator, :class_name => "User"
end
当我尝试使用创建者创建事件时,它显示 ActiveModel::UnknownAttributeError: unknown attribute 'creator_id' for Event.
我运行rails db:migrate,但它仍然没有创建外键并添加到事件表中。我究竟做错了什么?我到处看了看。
$ rails db:migrate:status
Status Migration ID Migration Name
--------------------------------------------------
up 20170625163737 Create users
up 20170625170905 Create events
up 20170625171959 Add description to event
up 20170625174531 Add creator id to events
但是,迁移文件显示没有添加:
class AddCreatorIdToEvents < ActiveRecord::Migration[5.1]
def change
end
end
【问题讨论】:
-
rails db:migrate:status返回什么? -
您能否使用迁移文件更新问题以创建事件表。您可以在
your_app/db/migrate下找到它。 -
@SebastiánPalma 添加了。
-
@Kranti 添加。它基本上什么都不做。
标签: ruby-on-rails