has_many的连接模型中的NameError:通过关联

时间:2017-02-01 18:59:57

标签: ruby-on-rails activerecord has-many-through

ItemLaserSheet模型具有多对多关系。我正在尝试使用联接模型has_many :through使用ItemLaserSheet关联来实现此功能。但是,当我运行rake db:seed时出现错误:

NameError: undefined local variable or method `laser_sheet_id=' for #<ItemLaserSheet id: nil, created_at: nil, updated_at: nil>

laser_sheet.rb

class LaserSheet < ActiveRecord::Base
  belongs_to :job
  has_many   :item_laser_sheets
  has_many   :items, through: :item_laser_sheets
  ...
end

item.rb的

class Item < ActiveRecord::Base
  has_many :item_laser_sheets
  has_many :laser_sheets,   through: :item_laser_sheets
  belongs_to :job
  ...
end

item_laser_sheet.rb

class ItemLaserSheet < ActiveRecord::Base
  belongs_to :item
  belongs_to :laser_sheet
end

20170201181546_create_item_laser_sheets.rb

class CreateItemLaserSheets < ActiveRecord::Migration
  def change
    create_table :item_laser_sheets do |t|
      t.belongs_to :item, index: true
      t.belongs_to :laser_sheet, index: true
      t.timestamps null: false
    end
  end
end

seeds.rb

...
jobs.each do |job|
  # Create laser sheets for job
  thickness = ["20ga", "18ga", "16ga", "14ga", "11ga", "0.187", "0.25"].sample
  material  = ["SS", "HRS", "CRS", "Alum", "MAG", "SS316"].sample
  creation_date = Faker::Date.backward(30)
  [1,2,3,4,5].sample.times do |n|
    sheet_name = job.name + " " + Date.today.to_s + " " + thickness + material + (n+1).to_s.rjust(2, '0')
    laser_sheet = job.laser_sheets.create!(name: sheet_name, created_at: creation_date)

    # Only apply cut_at datetime for random sheets
    if [true, true, true, true, false].sample
      laser_sheet.cut_at = Faker::Date.between(laser_sheet.created_at, Time.zone.now)
      laser_sheet.save!
    end

    # Create items for job
    [1,2,3].sample.times do |n|
      item_number = job.number.to_s + "-" + Faker::Number.between(1,100).to_s
      item_product_type = Item.product_types.keys.to_a.sample
      item = job.items.create(number: item_number, product_type: item_product_type)
      laser_sheet.items << item
      item.laser_sheets << laser_sheet
    end
  end
end

rake db:种子堆栈跟踪

NameError: undefined local variable or method `laser_sheet_id=' for #<ItemLaserSheet id: nil, created_at: nil, updated_at: nil>
/usr/local/rvm/gems/ruby-2.2.1/gems/activemodel-4.2.2/lib/active_model/attribute_methods.rb:433:in `method_missing'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/attribute_assignment.rb:54:in `public_send'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/attribute_assignment.rb:54:in `_assign_attribute'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/attribute_assignment.rb:41:in `block in assign_attributes'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/attribute_assignment.rb:35:in `each'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/attribute_assignment.rb:35:in `assign_attributes'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/associations/association.rb:169:in `initialize_attributes'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/associations/association.rb:248:in `block in build_record'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/core.rb:283:in `initialize'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/inheritance.rb:61:in `new'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/inheritance.rb:61:in `new'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/reflection.rb:131:in `build_association'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/associations/association.rb:247:in `build_record'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/associations/collection_association.rb:146:in `build'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/associations/has_many_through_association.rb:95:in `build_through_record'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/associations/has_many_through_association.rb:112:in `save_through_record'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/associations/has_many_through_association.rb:66:in `insert_record'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/associations/collection_association.rb:569:in `block (2 levels) in concat_records'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/associations/collection_association.rb:408:in `replace_on_target'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/associations/collection_association.rb:403:in `add_to_target'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/associations/collection_association.rb:568:in `block in concat_records'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/associations/collection_association.rb:566:in `each'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/associations/collection_association.rb:566:in `concat_records'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/associations/has_many_association.rb:173:in `concat_records'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/associations/has_many_through_association.rb:44:in `concat_records'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/associations/collection_association.rb:168:in `block in concat'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/associations/collection_association.rb:183:in `block in transaction'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/connection_adapters/abstract/transaction.rb:188:in `within_new_transaction'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/transactions.rb:220:in `transaction'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/associations/collection_association.rb:182:in `transaction'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/associations/collection_association.rb:168:in `concat'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/associations/has_many_through_association.rb:38:in `concat'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/associations/collection_proxy.rb:969:in `<<'
/home/ubuntu/workspace/job_tracker/db/seeds.rb:31:in `block (3 levels) in <top (required)>'
/home/ubuntu/workspace/job_tracker/db/seeds.rb:27:in `times'
/home/ubuntu/workspace/job_tracker/db/seeds.rb:27:in `block (2 levels) in <top (required)>'
/home/ubuntu/workspace/job_tracker/db/seeds.rb:16:in `times'
/home/ubuntu/workspace/job_tracker/db/seeds.rb:16:in `block in <top (required)>'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/relation/delegation.rb:46:in `each'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/relation/delegation.rb:46:in `each'
/home/ubuntu/workspace/job_tracker/db/seeds.rb:11:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `load'
/usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `block in load'
/usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:240:in `load_dependency'
/usr/local/rvm/gems/ruby-2.2.1/gems/activesupport-4.2.2/lib/active_support/dependencies.rb:268:in `load'
/usr/local/rvm/gems/ruby-2.2.1/gems/railties-4.2.2/lib/rails/engine.rb:547:in `load_seed'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/tasks/database_tasks.rb:250:in `load_seed'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/railties/databases.rake:180:in `block (2 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/railties/databases.rake:139:in `block (2 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `eval'
/usr/local/rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:setup => db:seed

1 个答案:

答案 0 :(得分:0)

Per Max的建议,我看了db/schema.rb

<强> schema.rb

ActiveRecord::Schema.define(version: 20170201181546) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "item_laser_sheets", force: :cascade do |t|
    t.datetime "created_at",     null: false
    t.datetime "updated_at",     null: false
  end
  ...
end

我之前更改了迁移以添加两个t.belongs_to行并运行rake db:reset,但没有意识到这不会重新生成架构。我运行了以下命令:

rake db:drop db:create db:migrate

消灭了数据库,重新创建了架构和数据库,并完成了迁移。然后我能够确认架构已更新:

更新了schema.rb

ActiveRecord::Schema.define(version: 20170201181546) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "item_laser_sheets", force: :cascade do |t|
    t.integer  "item_id"
    t.integer  "laser_sheet_id"
    t.datetime "created_at",     null: false
    t.datetime "updated_at",     null: false
  end
  ...
end
相关问题