Alan Coromano
Alan Coromano

Reputation: 26048

Unable to make a rake-tasks file and make it work properly

I decided to create a rake tasks for my Sinatra project and not to use the ready ones.

#Rakefile

 require 'rake/testtask'
 require 'rake/clean'
 Dir.glob("tasks/*.rake").each { |r| import r }

#/tasks/seed.rake
require 'rubygems'
require 'bundler'
Bundler.require
require 'mongoid'
require_relative '../models/user'

namespace :db do
  task :seed do
    puts 'Creating a user....'

    user1 = User.new email: "[email protected]", password: "test123"
    user1.save!

    puts 'User has been created.'
  end
end

#user.rb

require 'bcrypt'
require 'digest/md5'
require 'openssl'

class User
  include Mongoid::Document
  include Mongoid::Timestamps
  #.........

#gemfile (partly)
source 'http://rubygems.org'
gem 'bcrypt-ruby', require: 'bcrypt'

And I've got the error of "Creating a user....

rake aborted!
undefined method `create!' for BCrypt::Password:Class
/home/alex/ruby_projects/service/models/user.rb:47:in `password='"

where #47 looks like

  def password= pass
    self.hashed_password = BCrypt::Password.create! pass, cost: 10
  end

Note that in normal development everything works just fine. So I missed to require a file I think.

Your thoughts?

p.s. Even if I put

   require 'bcrypt'
    require 'digest/md5'
    require 'openssl

to /tasks/seed.rake the error remains.

Upvotes: 1

Views: 179

Answers (1)

Stuart M
Stuart M

Reputation: 11588

It appears you are using a non-existant method from BCrypt::Password. According to the docs, there is only a .create method and no .create! method. Switch to BCrypt::Password.create and it should work.

def password= pass
  self.hashed_password = BCrypt::Password.create pass, cost: 10
end

Upvotes: 1

Related Questions