ActiveModel :: UnknownAttributeError:播种归属关系时,未知属性(ActiveModel::UnknownAttributeError: unknown attribute when seeding belongs_to relationship)

47 IT屋

Trying to seed a has_many/belongs_to relationship. A user has_many scripts, a script belongs_to a user.

When seeding the script, I get this error:

ActiveModel::UnknownAttributeError: unknown attribute 'user_id' for Script.

I'd think that this line in my migration would create a user_id attribute for script?

t.belongs_to :user, index: true, foreign_key: true

app/models/script.rb

class Script < ApplicationRecord
  belongs_to :user
  has_many :commits
  has_many :script_users, through: :commits, source: :user
end

app/models/user.rb

class User < ActiveRecord::Base
  has_many :scripts
  has_many :used_scripts, through: :commits, source: :script
  has_many :commits
end

db/migrate/20171231022826_create_scripts.rb

class CreateScripts < ActiveRecord::Migration[5.1]
  def change
    create_table :scripts do |t|
      t.string :name
      t.string :skill
      t.string :bot_for
      t.string :game_for

      t.belongs_to :user, index: true, foreign_key: true

      t.timestamps
    end
  end
end

db/seeds.rb

admin = User.create!(
  email: 'a@a.com',
  password: 'adminadmin',
  password_confirmation: 'adminadmin'
)

script = Script.create!(
  name: 'YWoodcutter',
  skill: 'Woodcutting',
  bot_for: 'Android',
  game_for: "CandyLand,
  user_id: admin.id
)

ActiveModel::UnknownAttributeError: unknown attribute 'user_id' for Script. I've made sure to rake:db reset and still get error.

Thank you

UPDATE:

I did a few things and it's working OK now. I believe the issue may have been that my user migration was created after my script migration. The user migration should done before scripts, so I changed the dates to change the order they were migrated in. Did a rake rake db:rollback & db:reset for good measure too.

解决方案

belongs_to is an alias of reference and it should work same as references

If its not working for you just try with following solution

class CreateScripts < ActiveRecord::Migration[5.1]
  def change
    create_table :scripts do |t|
      t.string :name
      t.string :skill
      t.string :bot_for
      t.string :game_for

      t.references :user, index: true, foreign_key: true

      t.timestamps
    end
  end
end

尝试建立has_many / belongs_to关系。
一个用户有多个脚本,一个脚本属于一个用户。



播种该脚本时,出现此错误:




ActiveModel :: UnknownAttributeError:
脚本的未知属性'user_id'。




< p>我认为迁移中的这一行会为脚本创建user_id属性吗?



  t.belongs_to:user,索引:true,外键:true 


app / models / script.rb



 类脚本< ApplicationRecord 
属于:user
has_many:commits
has_many:script_users,通过::commits,来源::user
end


app / models / user.rb



  class用户< ActiveRecord :: Base 
has_many:scripts
has_many:used_scripts,通过::commits,源::script
has_many:commits
end


db / migrate / 20171231022826_create_scripts.rb



  class CreateScripts< ActiveRecord :: Migration [5.1] 
def更改
create_table:scripts do | t |
t.string:name
t.string:skill
t.string:bot_for
t.string:game_for

t.belongs_to:user, index:true,外键:true

t.timestamps
结束
结束
结束


db / seeds.rb



  admin = User.create!(
电子邮件:'a@a.com',
密码:'adminadmin',
password_confirmation:'adminadmin'


脚本= Script.create!(
名称:" YWoodcutter",
技能:" Woodcutting",
bot_for:" Android",
game_for:" CandyLand,
user_id:admin.id




ActiveModel :: UnknownAttributeError:
脚本的未知属性'user_id'。
我已经确保进行rake:db重置并仍然出错。




谢谢



更新:



我做了一个几件事,现在可以正常工作了。我相信iss可能是因为我的用户迁移是在脚本迁移之后创建的。用户迁移应该在脚本之前完成,因此我更改了日期以更改其迁移顺序。rake rake db:rollback&


解决方案

belongs_to 是别名引用的值,它应与引用

相同。

如果对您不起作用,请尝试以下解决方案



  class CreateScripts< ActiveRecord :: Migration [5.1] 
def更改
create_table:scripts do | t |
t.string:name
t.string:skill
t.string:bot_for
t.string:game_for

t.references:user, index:true,外键:true

t.timestamps
end
end
end

本文地址:IT屋 » ActiveModel :: UnknownAttributeError:播种归属关系时,未知属性