How to fill in the created user

I want to populate db with fake data using fireworks and placeholder stones. Using Devise created the User model.

this is my rake file

namespace :db do desc "Fill database with sample data" task populate: :environment do [User, Article].each(&:delete_all) password = "password" User.populate 20 do |user| user.name = Faker::Name.name user.email = Faker::Internet.email user.password = password user.password_confirmation = password Article.populate 5 do |article| article.user_id = user.id article.title = Populator.words(1..3).titleize article.content = Populator.sentences(2..10) article.created_at = 2.years.ago..Time.now end end end end 

When I run rake db: populate calls the following

 rake aborted! undefined method `password=' for #<Populator::Record:0xa179d14> /home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/populator-1.0.0/lib/populator/record.rb:64:in `method_missing' /home/sunloverz/RubymineProjects/socialnews/lib/tasks/sample_date.rake:10:in `block (3 levels) in <top (required)>' /home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/populator-1.0.0/lib/populator/factory.rb:53:in `call' /home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/populator-1.0.0/lib/populator/factory.rb:53:in `block in build_records' /home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/populator-1.0.0/lib/populator/factory.rb:50:in `times' /home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/populator-1.0.0/lib/populator/factory.rb:50:in `build_records' /home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/populator-1.0.0/lib/populator/factory.rb:43:in `block in populate' /home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/populator-1.0.0/lib/populator/factory.rb:29:in `remember_depth' /home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/populator-1.0.0/lib/populator/factory.rb:42:in `populate' /home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/populator-1.0.0/lib/populator/model_additions.rb:25:in `populate' /home/sunloverz/RubymineProjects/socialnews/lib/tasks/sample_date.rake:6:in `block (2 levels) in <top (required)>' /home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/rake-10.0.4/lib/rake/task.rb:246:in `call' /home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/rake-10.0.4/lib/rake/task.rb:246:in `block in execute' /home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/rake-10.0.4/lib/rake/task.rb:241:in `each' /home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/rake-10.0.4/lib/rake/task.rb:241:in `execute' /home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/rake-10.0.4/lib/rake/task.rb:184:in `block in invoke_with_call_chain' /home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/rake-10.0.4/lib/rake/task.rb:177:in `invoke_with_call_chain' /home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/rake-10.0.4/lib/rake/task.rb:170:in `invoke' /home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/rake-10.0.4/lib/rake/application.rb:143:in `invoke_task' /home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/rake-10.0.4/lib/rake/application.rb:101:in `block (2 levels) in top_level' /home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/rake-10.0.4/lib/rake/application.rb:101:in `each' /home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/rake-10.0.4/lib/rake/application.rb:101:in `block in top_level' /home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/rake-10.0.4/lib/rake/application.rb:110:in `run_with_threads' /home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/rake-10.0.4/lib/rake/application.rb:95:in `top_level' /home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/rake-10.0.4/lib/rake/application.rb:73:in `block in run' /home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/rake-10.0.4/lib/rake/application.rb:160:in `standard_exception_handling' /home/sunloverz/.rvm/gems/ruby-1.9.3-p385/gems/rake-10.0.4/lib/rake/application.rb:70:in `run' Tasks: TOP => db:populate (See full trace by running task with --trace) 

What's wrong?

+4
source share
2 answers

The filler does not load an ActiveRecord instance; therefore, any checks, callbacks, built-in methods will not work. Instead, it loads its own Record object, using column attributes and pure SQL to improve performance.

On the other hand, Devise does not create a password column, it creates an encrypted_password column and runs all the logic behind the scene, check this link if you want to get an idea of โ€‹โ€‹code development.

Solution: we need to call password_digest to encrypt this password, and then set encrypted_password directly, but this method is protected and cannot be called inside the rake. Instead, we will use the โ€œnewโ€ method to run password_digest, so your code will look something like this:

 password = "password" User.populate 20 do |user| user.name = Faker::Name.name user.email = Faker::Internet.email user.encrypted_password = User.new(:password => password).encrypted_password # rest of your code here end 
+6
source

Make sure your model (table in your case) actually has a column called "password" .

If so, try checking if it has attr_accessible :password

0
source

All Articles