Ruby on rails ActiveModel::MissingAttributeError:can';无法写入未知属性'user\u id`?
我有一个联盟的模式Ruby on rails ActiveModel::MissingAttributeError:can';无法写入未知属性'user\u id`?,ruby-on-rails,model,associations,Ruby On Rails,Model,Associations,我有一个联盟的模式 create_table "leagues", force: :cascade do |t| t.string "name", null: false t.date "start_date", null: false t.date "end_date" t.string "day" t.string "start_
create_table "leagues", force: :cascade do |t|
t.string "name", null: false
t.date "start_date", null: false
t.date "end_date"
t.string "day"
t.string "start_time", null: false
t.integer "practice_length", null: false
t.integer "alley_id", null: false
t.integer "frequency", null: false
t.integer "scratch", default: 220
t.integer "handicap_percent", default: 80
t.integer "handicap_round", default: 0
t.integer "occurrences", null: false
t.integer "user_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
和模型
class Alley < ActiveRecord::Base
belongs_to :address
has_many :leagues
end
class User < ActiveRecord::Base
has_many :leagues
end
class League < ActiveRecord::Base
belongs_to :alley
belongs_to :user
has_many :teams
end
当我尝试创建联盟时,我会出错
ActiveModel::缺少属性错误:
无法写入未知属性user\u id
它的设置和alley一模一样,alley工作正常。一个联盟应该分配给一条巷子,分配给一个用户。一条巷子可以有很多联盟,一个用户可以有很多联盟。坦白地说,我太累了,无法弄清楚为什么现在这样做不起作用。我一直在修改迁移(当我开始创建初始数据集时),我先向下迁移,然后向上迁移。这就是我所处的位置。我不得不删除数据库,然后运行安装程序。一定是什么东西过时了。就我而言,我的测试失败了。多亏了@TIMBERings answers,我才得以缩小范围 如果创建的迁移没有索引,请向前迁移,添加 索引(或引用)到迁移文件,迁移或回滚, 再向前移动,你就搞砸了 为了修复失败的RSpec测试,我运行了以下程序并重新运行测试,结果通过了
rails db:reset RAILS_ENV=test
你也可以直接跑
rails db:reset
你能为
user
更新工厂吗?是的,我可以更新它,更新到什么?是的,我只是想看得更清楚,顺便说一句,你可以重新检查模式中的user\u id
,ActiveModel::MissingAttributeError
表示缺少user\u id
之类的信息!您可以执行rake db:reset,以验证您的假设。如果创建迁移时没有索引,请向前迁移,将索引(或引用)添加到迁移文件中,然后进行迁移或回滚,然后再次向前迁移,您将陷入混乱。Rails团队不认为这是一个可接受的用例,所以不要期望得到修复。
rails db:reset