这是我的 rake 任务文件:
namespace :db do
desc "fill database with sample data"
task :populate => :environment do
puts "populating with sample data"
make_users
make_companies
make_products
make_images
make_commits
end
end
def make_users
100.times do |user|
User.create!(first_name: Faker::Name.first_name,
last_name: Faker::Name.last_name,
email: Faker::Internet.email,
question_response: "ques"+(user).to_s,
device_id: 100,
company_name:"somecompany"+(user).to_s)
end
end
def make_companies
100.times do |company|
name = Faker::Name.first_name
rep_name = "example"+(company).to_s
rep_designation = "example_desig"+(company).to_s
company_large_logo = "example_lg"+(company).to_s
company_small_logo ="example_sg"+(company).to_s
company_social_contacts = "example_csc"+(company).to_s
company_writeup = "example_wr"+(company).to_s
website = "exampleCompany"+(company).to_s
rep_image = "example_image"+(company).to_s
active = 1
average = 10
Company.create!(name: name,
rep_name: rep_name,
rep_designation: rep_designation,
company_social_contacts: company_social_contacts,
company_large_logo: company_large_logo,
company_small_logo: company_small_logo,
company_writeup: company_writeup,
website:website,
rep_image: rep_image,
active:active,
average: average)
end
end
def make_products
companies=Company.all
100.times do |product|
name = Faker::Name.name
image_url = "some_image_url"+(product+1).to_s
description = "some_description"+(product+1).to_s
website = "some_website"+(product+1).to_s
active = 0
unlocked_time = "time"+(product+1).to_s
company_id = companies.sample.id
#aggregate = product+10
companies.sample.products.create!(name: name,
image_url: image_url,
description: description,
website: website,
active: active,
unlocked_time: unlocked_time,
company_id: company_id,
aggregate: 0)
end
end
def make_commits
users = User.all
products = Product.all
100.times do |commit|
user_selected=users.sample
product_selected=products.sample
user_selected.commits.create!(product_id: product_selected.id,
user_id: user_selected.id,
estimate: commit*2,
commit: commit+1)
end
end
def make_images
products = Product.all
100.times do |image|
products.sample.images.create!(product_id: products.sample.id,
file: "sample_image#{image+1}")
end
end
当我运行 rakedb:populate
时,我收到了这个堆栈跟踪错误。我怎样才能解决这个问题?
堆栈跟踪:
populating with sample data
rake aborted!
undefined method `file_changed?' for #<Product:0x0000010493e6f8>
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activemodel-3.2.11/lib/active_model/attribute_methods.rb:407:in `method_missing'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/attribute_methods.rb:149:in `method_missing'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/carrierwave-0.8.0/lib/carrierwave/mount.rb:249:in `store_previous_model_for_file'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:407:in `_run__4010438894236454661__update__1959316297081505070__callbacks'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:405:in `__run_callback'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:385:in `_run_update_callbacks'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:81:in `run_callbacks'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/callbacks.rb:272:in `update'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/persistence.rb:348:in `create_or_update'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/callbacks.rb:264:in `block in create_or_update'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:436:in `_run__4010438894236454661__save__1959316297081505070__callbacks'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:405:in `__run_callback'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:385:in `_run_save_callbacks'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:81:in `run_callbacks'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/callbacks.rb:264:in `create_or_update'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/persistence.rb:84:in `save'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/validations.rb:50:in `save'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/attribute_methods/dirty.rb:22:in `save'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/transactions.rb:259:in `block (2 levels) in save'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/transactions.rb:313:in `block in with_transaction_returning_status'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/transactions.rb:208:in `transaction'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/transactions.rb:311:in `with_transaction_returning_status'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/transactions.rb:259:in `block in save'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/transactions.rb:270:in `rollback_active_record_state!'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/transactions.rb:258:in `save'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/persistence.rb:217:in `block in update_attributes'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/transactions.rb:313:in `block in with_transaction_returning_status'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/transactions.rb:208:in `transaction'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/transactions.rb:311:in `with_transaction_returning_status'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/persistence.rb:215:in `update_attributes'
/Users/test/Launch/launch/app/models/product.rb:61:in `aggregate_more'
/Users/test/Launch/launch/app/models/commit.rb:30:in `aggregate_commit'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:427:in `_run__2272083058160445442__save__1959316297081505070__callbacks'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:405:in `__run_callback'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:385:in `_run_save_callbacks'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:81:in `run_callbacks'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/callbacks.rb:264:in `create_or_update'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/persistence.rb:104:in `save!'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/validations.rb:56:in `save!'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/attribute_methods/dirty.rb:33:in `save!'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/transactions.rb:264:in `block in save!'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/transactions.rb:313:in `block in with_transaction_returning_status'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/transactions.rb:208:in `transaction'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/transactions.rb:311:in `with_transaction_returning_status'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/transactions.rb:264:in `save!'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/associations/has_many_association.rb:14:in `insert_record'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:436:in `block (2 levels) in create_record'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:344:in `add_to_target'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:434:in `block in create_record'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:149:in `block in transaction'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/transactions.rb:208:in `transaction'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:148:in `transaction'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:433:in `create_record'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:123:in `create!'
/Users/test/.rvm/gems/ruby-1.9.3-p362/gems/activerecord-3.2.11/lib/active_record/associations/collection_proxy.rb:46:in `create!'
/Users/test/Launch/launch/lib/tasks/sample_data.rake:107:in `block in make_commits'
/Users/test/Launch/launch/lib/tasks/sample_data.rake:102:in `times'
/Users/test/Launch/launch/lib/tasks/sample_data.rake:102:in `make_commits'
/Users/test/Launch/launch/lib/tasks/sample_data.rake:16:in `block (2 levels)
/Users/test/.rvm/gems/ruby-1.9.3-p362/bin/ruby_noexec_wrapper:14:in `eval'
/Users/test/.rvm/gems/ruby-1.9.3-p362/bin/ruby_noexec_wrapper:14:in main
Tasks: TOP => db:populate
(See full trace by running task with --trace)
我也在使用载波 gem 来上传图片。