[英]seeding a db in ruby on rails, can't initialize a object because of a method missing I don't use
I'm have a model that consists in User has and belongs to many brands that have many products that have many categories. 我有一个模型,其中包含“用户拥有”并且属于许多品牌,这些品牌的许多产品具有很多类别。 Code:
码:
class User < ActiveRecord::Base
has_and_belongs_to_many :brands, dependent: :destroy
accepts_nested_attributes_for :brands
validates_presence_of :brands
end
class Brand < ActiveRecord::Base
has_and_belongs_to_many :users
has_many :products, dependent: :destroy
validates :name, presence: true,
length: { maximum: 50 }
end
class Product < ActiveRecord::Base
belongs_to :brand
has_many :categories, dependent: :destroy
validates :price, presence: true
validates :brand_id, presence: true
validates :name, presence: true,
length: { maximum: 50 }
end
class Category < ActiveRecord::Base
belongs_to :product
validates :product_id, presence: true
validates :name, presence: true,
length: { maximum: 50 }
end
SCHEMA: 模式:
ActiveRecord::Schema.define(version: 20160617235015) do
create_table "brands", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "brands_users", id: false, force: :cascade do |t|
t.integer "brand_id"
t.integer "user_id"
end
add_index "brands_users", ["brand_id"], name: "index_brands_users_on_brand_id"
add_index "brands_users", ["user_id"], name: "index_brands_users_on_user_id"
create_table "categories", force: :cascade do |t|
t.string "name"
t.decimal "price"
t.boolean "out_of_stock", default: false
t.integer "product_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "categories", ["product_id"], name: "index_categories_on_product_id"
create_table "products", force: :cascade do |t|
t.string "name"
t.integer "brand_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "products", ["brand_id"], name: "index_products_on_brand_id"
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.string "remember_digest"
t.boolean "admin", default: false
t.string "activation_digest"
t.boolean "activated", default: false
t.datetime "activated_at"
t.string "reset_digest"
t.datetime "reset_sent_at"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
end
The brand and user part works fine and was a previous implementation to my code. 品牌和用户部分工作正常,并且是我代码的先前实现。 When I tried to seed my db using rake db:seed with this:
当我尝试使用rake db:seed将我的数据库作为种子时:
User.create!(name: "Foobar",
email: "foobar@foobar.com",
password: 'foobarbaz',
password_confirmation: 'foobarbaz',
admin: true,
activated: true,
activated_at: Time.zone.now,
brands_attributes: [name: 'Bar'])
User.create!(name: "Foo",
email: "foo@bar.com",
password: 'foobarbaz',
password_confirmation: 'foobarbaz',
admin: false,
activated: true,
activated_at: Time.zone.now,
brands_attributes: [name: 'Foo'])
brand = Brand.second
15.times do
name = Faker::Commerce.product_name
brand.products.create!(name: name)
end
products = Product.take(10)
3.times do
name = Faker::Space.star
price = 1
products.each { |product| product.categories.create!(name: name,
price: price) }
end
50.times do |n|
name = Faker::Name.name
email = "example-#{n+1}@foobar.org"
password = "password"
brand = Faker::Company.name
User.create!(name: name,
email: email,
password: password,
password_confirmation: password,
activated: true,
activated_at: Time.zone.now,
brands_attributes: [name: brand])
end
I get this error: 我收到此错误:
rake aborted!
NoMethodError: undefined method `price' for #<Product:0x007ff77baa7b50>
<path>/.rvm/gems/ruby-2.3.1/gems/activemodel-4.2.6/lib/active_model/attribute_methods.rb:433:in `method_missing'
<path>/.rvm/gems/ruby-2.3.1/gems/activemodel-4.2.6/lib/active_model/validator.rb:149:in `block in validate'
<path>/.rvm/gems/ruby-2.3.1/gems/activemodel-4.2.6/lib/active_model/validator.rb:148:in `each'
<path>/.rvm/gems/ruby-2.3.1/gems/activemodel-4.2.6/lib/active_model/validator.rb:148:in `validate'
<path>/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/validations/presence.rb:5:in `validate'
<path>/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:455:in `public_send'
<path>/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:455:in `block in make_lambda'
<path>/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:192:in `block in simple'
<path>/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:504:in `block in call'
<path>/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:504:in `each'
<path>/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:504:in `call'
<path>/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:92:in `__run_callbacks__'
<path>/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:778:in `_run_validate_callbacks'
<path>/.rvm/gems/ruby-2.3.1/gems/activemodel-4.2.6/lib/active_model/validations.rb:399:in `run_validations!'
<path>/.rvm/gems/ruby-2.3.1/gems/activemodel-4.2.6/lib/active_model/validations/callbacks.rb:113:in `block in run_validations!'
<path>/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:88:in `__run_callbacks__'
<path>/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:778:in `_run_validation_callbacks'
<path>/.rvm/gems/ruby-2.3.1/gems/activemodel-4.2.6/lib/active_model/validations/callbacks.rb:113:in `run_validations!'
<path>/.rvm/gems/ruby-2.3.1/gems/activemodel-4.2.6/lib/active_model/validations.rb:338:in `valid?'
<path>/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/validations.rb:58:in `valid?'
<path>/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/validations.rb:83:in `perform_validations'
<path>/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/validations.rb:43:in `save!'
<path>/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/attribute_methods/dirty.rb:29:in `save!'
<path>/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/transactions.rb:291:in `block in save!'
<path>/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/transactions.rb:351:in `block in with_transaction_returning_status'
<path>/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `transaction'
<path>/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/transactions.rb:220:in `transaction'
<path>/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/transactions.rb:348:in `with_transaction_returning_status'
<path>/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/transactions.rb:291:in `save!'
<path>/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/associations/has_many_association.rb:39:in `insert_record'
<path>/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/associations/collection_association.rb:495:in `block (2 levels) in _create_record'
<path>/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/associations/collection_association.rb:408:in `replace_on_target'
<path>/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/associations/collection_association.rb:403:in `add_to_target'
<path>/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/associations/collection_association.rb:493:in `block in _create_record'
<path>/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/associations/collection_association.rb:183:in `block in transaction'
<path>/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
<path>/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
<path>/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
<path>/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/transactions.rb:220:in `transaction'
<path>/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/associations/collection_association.rb:182:in `transaction'
<path>/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/associations/collection_association.rb:492:in `_create_record'
<path>/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/associations/has_many_association.rb:187:in `_create_record'
<path>/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/associations/collection_association.rb:157:in `create!'
<path>/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/associations/collection_proxy.rb:306:in `create!'
<path>/seeds.rb:22:in `block in <top (required)>'
<path>/seeds.rb:20:in `times'
<path>/seeds.rb:20:in `<top (required)>'
<path>/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `load'
<path>/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `block in load'
<path>/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:240:in `load_dependency'
<path>/.rvm/gems/ruby-2.3.1/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `load'
<path>/.rvm/gems/ruby-2.3.1/gems/railties-4.2.6/lib/rails/engine.rb:547:in `load_seed'
<path>/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/tasks/database_tasks.rb:250:in `load_seed'
<path>/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.6/lib/active_record/railties/databases.rake:183:in `block (2 levels) in <top (required)>'
<path>/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `eval'
<path>/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:seed
I have tried a lot but I can't find any mistakes nor solutions. 我已经尝试了很多,但是找不到任何错误或解决方案。 I don't use price anywhere except in the column from categories.
除了类别列中的任何地方,我都不会使用价格。 Any help is helpful as Im a begginer.
任何帮助都是有益的,因为我是初学者。
You'r schema doesnt show price. 您的架构不显示价格。 thats why its giving you
undefined method
price'` 那就是为什么它给您
undefined method
价格'`
create_table "products", force: :cascade do |t|
t.string "name"
t.integer "brand_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
Most likely you are getting this error due to the following validation on your Product model: 由于您对产品模型进行了以下验证,很可能会收到此错误:
validates :price, presence: true
Try to remove it. 尝试将其删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.