views:

300

answers:

3

I'm pretty sure I understand how seeding work with respect to seeds.rb, but I can't seem to use it to stick a restful_authentication User object into the database.

User.create(:login => 'admin',
            :role => Role.find_by_name('super_admin'),
            :email => '[email protected]',
            :password => '123123')

Am I missing something?

Edit: I also tried adding the password confirmation. Still nothing.

+1  A: 

Password confirmation?

Gordon Isnor
Unfortunately, it didn't work with that either.
mindeavor.
You may want to just check the validations and/or try to create this with the same attributes in the console and then check errors
Gordon Isnor
+2  A: 

Try User.create!() using the same parameters). This will show you any validation errors in the console.

Beerlington
+1  A: 

Have you turned the notification on? If so, the User model is trying to send the email notification. If you haven't configured your email server, the create operation will fail.

I had to do the following to work around the problem.

1 Modify the User model

Add a virtual attribute called dont_notify.

class User < ActiveRecord::Base
   # add a attribute
   attr_accessor dont_notify
end

2 Change the Observer code to detect the attribute.

class UserObserver < ActiveRecord::Observer

  def after_create(user)
    return if user.dont_notify? #notice this line 
    UserMailer.deliver_signup_notification(user)
  end

  def after_save(user)
    return if user.dont_notify? #notice this line
    UserMailer.deliver_activation(user) if user.recently_activated?
  end
end

3 Set the dont_notify flag during seeding

In you seed.rb set the flag.

User.create(:login => 'admin',
            :role => Role.find_by_name('super_admin'),
            :email => '[email protected]',
            :password => '123123',
            :password_confirmation => '123123',
            :dont_notify => true
         )
KandadaBoggu