尝试在ubuntu和postgresql(sudo rake db:seed
)中使用Ruby on Rails进行种子播放,并且它不断抛出uninitialized constant
错误。这就是我得到的:
boris@alpha:~/Desktop/wikiful$ sudo rake db:seed --trace
[sudo] password for boris:
** Invoke db:seed (first_time)
** Execute db:seed
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:abort_if_pending_migrations
rake aborted!
uninitialized constant Category::Articles
/usr/local/lib/ruby/gems/2.0.0/gems/activerecord-
4.0.2/lib/active_record/inheritance.rb:125:in `compute_type'
/usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/reflection.rb:178:in `klass'
/usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/associations/association.rb:123:in `klass'
/usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/associations/collection_association.rb:37:in `reader'
/usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/associations/builder/association.rb:70:in `articles'
/usr/local/lib/ruby/gems/2.0.0/gems/activemodel-4.0.2/lib/active_model/validator.rb:151:in `block in validate'
/usr/local/lib/ruby/gems/2.0.0/gems/activemodel-4.0.2/lib/active_model/validator.rb:150:in `each'
/usr/local/lib/ruby/gems/2.0.0/gems/activemodel-4.0.2/lib/active_model/validator.rb:150:in `validate'
/usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/validations/presence.rb:5:in `validate'
/usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:283:in `_callback_before_7'
/usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:397:in `_run__1018118611__validate__callbacks'
/usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:80:in `run_callbacks'
/usr/local/lib/ruby/gems/2.0.0/gems/activemodel-4.0.2/lib/active_model/validations.rb:373:in `run_validations!'
/usr/local/lib/ruby/gems/2.0.0/gems/activemodel-4.0.2/lib/active_model/validations/callbacks.rb:106:in `block in run_validations!'
/usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:373:in `_run__1018118611__validation__callbacks'
/usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:80:in `run_callbacks'
/usr/local/lib/ruby/gems/2.0.0/gems/activemodel-4.0.2/lib/active_model/validations/callbacks.rb:106:in `run_validations!'
/usr/local/lib/ruby/gems/2.0.0/gems/activemodel-4.0.2/lib/active_model/validations.rb:314:in `valid?'
/usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/validations.rb:70:in `valid?'
/usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/validations.rb:77:in `perform_validations'
/usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/validations.rb:51:in `save'
/usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/attribute_methods/dirty.rb:32:in `save'
/usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/transactions.rb:270:in `block (2 levels) in save'
/usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/transactions.rb:326:in `block in with_transaction_returning_status'
/usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction'
/usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction'
/usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction'
/usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/transactions.rb:209:in `transaction'
/usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/transactions.rb:323:in `with_transaction_returning_status'
/usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/transactions.rb:270:in `block in save'
/usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/transactions.rb:281:in `rollback_active_record_state!'
/usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/transactions.rb:269:in `save'
/usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/persistence.rb:37:in `create'
/usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/persistence.rb:34:in `block in create'
/usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/persistence.rb:34:in `collect'
/usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/persistence.rb:34:in `create'
/home/boris/Desktop/wikiful/db/seeds.rb:5:in `<top (required)>'
/usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:223:in `load'
/usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:223:in `block in load'
/usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:214:in `load_dependency'
/usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:223:in `load'
/usr/local/lib/ruby/gems/2.0.0/gems/railties-4.0.2/lib/rails/engine.rb:540:in `load_seed'
/usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/tasks/database_tasks.rb:154:in `load_seed'
/usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/railties/databases.rake:181:in `block (2 levels) in <top (required)>'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:236:in `call'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:236:in `block in execute'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:231:in `each'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:231:in `execute'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:175:in `block in invoke_with_call_chain'
/usr/local/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:168:in `invoke_with_call_chain'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:161:in `invoke'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:149:in `invoke_task'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:106:in `each'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:106:in `block in top_level'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:115:in `run_with_threads'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:100:in `top_level'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:78:in `block in run'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:165:in `standard_exception_handling'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:75:in `run'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/bin/rake:33:in `<top (required)>'
/usr/local/bin/rake:23:in `load'
/usr/local/bin/rake:23:in `<main>'
Tasks: TOP => db:seed
这是我的种子文件:
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
# Create 8 seed categories
categories = Category.create([
{ name: 'History'}, {name: 'Biology'}, {name: 'Literature'},
{ name: 'Mathematics'}, { name: 'Music Theory'}, { name: 'Computer Science'},
{ name: 'Sociology'}, {name: 'Chemistry'}
])
# create 50 articles, with random titles, 250 words of content, and
# randomly assign one of the categories above to each article
for i in 0..49
title = Faker::Lorem.sentence(rand(2..10)).chomp('.')
content = Faker::Lorem.paragraph(word_count=250)
# randomly assign one of the categories we just created
category = Category.first(offset: rand(Category.count))
a = Article.create(title: title, content: content, categories: [category,])
end
类别模型:
class Category < ActiveRecord::Base
validates :name, presence: true
validates :name, uniqueness: true
has_many :article_categories
has_many :articles, through: :article_categories
end
articlecategory模型:
class ArticleCategory < ActiveRecord::Base
belongs_to :articles
belongs_to :categories
end
文章模型:
class Article < ActiveRecord::Base
belongs_to :user
validates :title, presence: true
validates :content, presence: true
has_many :article_categories
has_many :categories, through: :article_categories
end
建议 谢谢
答案 0 :(得分:0)
我很好奇你的模特是什么样的。假设您可以将类别附加到categories
模型中的Article
属性,那么您会犯一些错误。您应该拥有ArticleCategory
模型,其中has many through association具有类别和文章模型。因此,不应该为categories
属性分配类别,而应该执行类似的操作。
ArticleCategory.create(article_id: a.id, category_id: category.id)
在你的最后一行。
答案 1 :(得分:0)
您的加入模式错误:
class ArticleCategory < ActiveRecord::Base
belongs_to :article # not articles
belongs_to :category # not categories
end