Reputation: 165
I was trying to integrate twitter authentication into my Ruby on Rails application (from the Ruby on Rails Tutorial).
As a result I dropped a table (user table ) and had to recreate it. I want to now populate the database and I have this code for populating the table
namespace :db do
desc "Fill database with sample data"
task populate: :environment do
User.create!(name: "Example User",
email: "[email protected]",
password: "foobar",
password_confirmation: "foobar")
99.times do |n|
name = Faker::Name.name
email = "example-#{n+1}@railstutorial.org"
password = "password"
User.create!(name: name,
email: email,
password: password,
password_confirmation: password)
end
end
end
The following commands work:
$bundle exec rake:db migrate
$bundle exec rake:db reset
However the $bundle exec rake:db populate does NOT work.
There are the errors I am getting:
$ bundle exec rake db:populate --trace
** Invoke db:populate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:populate
rake aborted!
wrong number of arguments (0 for 1)
c:/Ruby193/lib/ruby/gems/1.9.1/gems/devise-2.0.4/lib/devise/models/database_auth
enticatable.rb:105:in `password_digest'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activemodel-3.2.0/lib/active_model/errors.rb
:254:in `block in add_on_blank'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activemodel-3.2.0/lib/active_model/errors.rb
:253:in `each'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activemodel-3.2.0/lib/active_model/errors.rb
:253:in `add_on_blank'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activemodel-3.2.0/lib/active_model/validatio
ns/presence.rb:9:in `validate'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.0/lib/active_support/callb
acks.rb:310:in `_callback_before_39'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.0/lib/active_support/callb
acks.rb:484:in `_run__584816360__validate__204154762__callbacks'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.0/lib/active_support/callb
acks.rb:405:in `__run_callback'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.0/lib/active_support/callb
acks.rb:385:in `_run_validate_callbacks'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.0/lib/active_support/callb
acks.rb:81:in `run_callbacks'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activemodel-3.2.0/lib/active_model/validatio
ns.rb:212:in `run_validations!'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activemodel-3.2.0/lib/active_model/validatio
ns/callbacks.rb:53:in `block in run_validations!'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.0/lib/active_support/callb
acks.rb:425:in `_run__584816360__validation__204154762__callbacks'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.0/lib/active_support/callb
acks.rb:405:in `__run_callback'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.0/lib/active_support/callb
acks.rb:385:in `_run_validation_callbacks'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.0/lib/active_support/callb
acks.rb:81:in `run_callbacks'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activemodel-3.2.0/lib/active_model/validatio
ns/callbacks.rb:53:in `run_validations!'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activemodel-3.2.0/lib/active_model/validatio
ns.rb:179:in `valid?'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.0/lib/active_record/validat
ions.rb:69:in `valid?'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.0/lib/active_record/validat
ions.rb:77:in `perform_validations'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.0/lib/active_record/validat
ions.rb:56:in `save!'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.0/lib/active_record/attribu
te_methods/dirty.rb:33:in `save!'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.0/lib/active_record/transac
tions.rb:246:in `block in save!'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.0/lib/active_record/transac
tions.rb:295:in `block in with_transaction_returning_status'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.0/lib/active_record/connect
ion_adapters/abstract/database_statements.rb:190:in `transaction'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.0/lib/active_record/transac
tions.rb:208:in `transaction'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.0/lib/active_record/transac
tions.rb:293:in `with_transaction_returning_status'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.0/lib/active_record/transac
tions.rb:246:in `save!'
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.0/lib/active_record/validat
ions.rb:41:in `create!'
c:/tuffy/shopgodt/lib/tasks/sample_data.rake:4:in `block (2 levels) in <top (req
uired)>'
c:/Ruby193/lib/ruby/1.9.1/rake/task.rb:205:in `call'
c:/Ruby193/lib/ruby/1.9.1/rake/task.rb:205:in `block in execute'
c:/Ruby193/lib/ruby/1.9.1/rake/task.rb:200:in `each'
c:/Ruby193/lib/ruby/1.9.1/rake/task.rb:200:in `execute'
c:/Ruby193/lib/ruby/1.9.1/rake/task.rb:158:in `block in invoke_with_call_chain'
c:/Ruby193/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
c:/Ruby193/lib/ruby/1.9.1/rake/task.rb:151:in `invoke_with_call_chain'
c:/Ruby193/lib/ruby/1.9.1/rake/task.rb:144:in `invoke'
c:/Ruby193/lib/ruby/1.9.1/rake/application.rb:116:in `invoke_task'
c:/Ruby193/lib/ruby/1.9.1/rake/application.rb:94:in `block (2 levels) in top_lev
el'
c:/Ruby193/lib/ruby/1.9.1/rake/application.rb:94:in `each'
c:/Ruby193/lib/ruby/1.9.1/rake/application.rb:94:in `block in top_level'
c:/Ruby193/lib/ruby/1.9.1/rake/application.rb:133:in `standard_exception_handlin
g'
c:/Ruby193/lib/ruby/1.9.1/rake/application.rb:88:in `top_level'
c:/Ruby193/lib/ruby/1.9.1/rake/application.rb:66:in `block in run'
c:/Ruby193/lib/ruby/1.9.1/rake/application.rb:133:in `standard_exception_handlin
g'
c:/Ruby193/lib/ruby/1.9.1/rake/application.rb:63:in `run'
c:/Ruby193/bin/rake:32:in `<main>'
Tasks: TOP => db:populate
Any help on this matter is appreciated. Thanks again!
============================================================
Update 05/02/2012: Since I had wanted to add third party logins (facebook,twitter) I added the Devise gem but I am not sure which command I ran that resulted in these these two lines that were added to my app/models/User.rb file
:token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
Once I commented them out everything ran fine. I would like to understand though what these two lines are doing?
Upvotes: 2
Views: 5235
Reputation: 640
OK - I was having the same issues. I just finished up with the same tutorial and have spent the past 3 hrs pulling my hair out. Going through the Gem's code and everything made me realize what was up.
What's going on is this:
In ch.6 the tutorial asks you to add in 'has_secure_password'
to your user model. Strip this helper out of any model you're using with Devise. While Devise does not explicitly call that helper in the Gem, it's code pretty much mimics what 'has_secure_password' does.
After you do that, kill your server, rails console, etc. Start over and life should be happy once again.
Upvotes: 2
Reputation: 880
There are two hints to what's wrong
c:/tuffy/shopgodt/lib/tasks/sample_data.rake:4:in 'block (2 levels) in <top (required)>'
tells you it's on line 4 which is the call to create!
Coupled with
c:/Ruby193/lib/ruby/gems/1.9.1/gems/activemodel-3.2.0/lib/active_model/validations/presence.rb:9:in 'validate'
says that a validation is failing. Is there a validation expecting the presence of some field you're not providing? Check your User
model for validates :some_field_name, :presence => true
Upvotes: 3
Reputation: 34774
It looks like it could be something on the User
class itself. Possibly in the validation.
Have you got any custom validation in there or any other custom callbacks? It would be worth checking those.
You could do the User.create!
independently of the rake task in the console to confirm if that is the problem.
Upvotes: 2