The Item
and LaserSheet
models have a many-to-many relationship. I am attempting to implement this using a has_many :through
association using a join model ItemLaserSheet
. However, when I run rake db:seed
I get an error:
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:seed stack trace
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
Per Max's suggestion, I looked at 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
I had previously changed the migration to add the two t.belongs_to
lines and ran rake db:reset
, not realizing that this doesn't regenerate the schema. I ran the following commands:
rake db:drop db:create db:migrate
which wiped out the database, recreated the schema and database, and ran through the migrations. I was then able to confirm that the schema was updated:
updated 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.