如何填充设计用户

时间:2013-04-17 13:39:01

标签: ruby-on-rails devise

我想使用faker和populator gems用假数据填充数据库。 使用Devise生成模型用户。

这是我的rake文件

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

当我运行rake db:populate引发以下

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)

有什么问题?

2 个答案:

答案 0 :(得分:6)

Populator不加载ActiveRecord实例;所以任何验证,回调,嵌入式方法都行不通。相反,它使用列属性和纯SQL来加载自己的Record对象,以提高性能。

另一方面,Devise不会创建:password列,它会创建encrypted_pa​​ssword列并执行场景背后的所有逻辑,如果您想了解设计代码,请查看此link

解决方案:我们需要调用password_digest来加密给定的密码,然后直接设置encrypted_pa​​ssword,但此method受到保护,无法在rake中调用。相反,我们会使用'new'方法来触发password_digest,因此您的代码将是这样的:

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

答案 1 :(得分:0)

确保您的模型(案例中的表格)actually有一个名为"password"的列。

如果是,那么,尝试检查它是否有attr_accessible :password