【发布时间】:2015-09-12 09:46:54
【问题描述】:
当我尝试运行这些代码行时,我得到一个“无法写入未知属性competition_id 错误”:
notification = Notification.create(
user: subscription.tournament.user,
content: "#{subscription.user.full_name} a demandé à s'inscrire à #{subscription.tournament.name} dans la catégorie #{subscription.competition.category} ",
competition: subscription.competition
)
我不明白,因为我运行迁移添加竞争作为通知的外键:
class AddCompetitionToNotification < ActiveRecord::Migration
def change
add_reference :notifications, :competition, index: true, foreign_key: true
end
end
我的架构也成功更新,表通知似乎有外键竞争:
create_table "notifications", force: :cascade do |t|
t.integer "user_id"
t.string "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "read", default: false
t.integer "convocation_id"
t.integer "tournament_id"
t.integer "competition_id"
end
add_index "notifications", ["competition_id"], name: "index_notifications_on_competition_id", using: :btree
add_index "notifications", ["user_id"], name: "index_notifications_on_user_id", using: :btree
我的竞赛模型有以下关联:
class Competition < ActiveRecord::Base
belongs_to :tournament
has_many :subscriptions, dependent: :destroy
has_many :notifications, dependent: :destroy
我的通知模型
class Notification < ActiveRecord::Base
belongs_to :user
belongs_to :convocation
belongs_to :tournament
belongs_to :competition
end
这个错误来自哪里?
【问题讨论】:
-
您的
notification模型是否已经引用了competition?请向我们展示这两个模型。 -
刚刚更新了通知和竞赛协会的帖子
标签: ruby-on-rails migration database-schema