Big_Bird
Big_Bird

Reputation: 427

User Exists on creation, but it actually doesn't in the Database

I'm having a strange issue with the DB setup for my current application.

As the migrations pass without an issue, however when I try to Seed the DB with some test data it says the data exists and rolls back the transaction.

When I query the DB to check if the record exists it comes back with nil.

At this point I'm lost to where I should be even looking for the culprit.

Any help is appreciated.

seeds.rb:

User.create(first_name: 'John', last_name: 'Doe', email: '[email protected]', password: 'testing', is_admin: true)

rake db:seed output:

  ActiveRecord::SchemaMigration Load (0.5ms)  SELECT "schema_migrations".* FROM "schema_migrations"
   (0.2ms)  BEGIN
  User Exists (1.0ms)  SELECT  1 AS one FROM "users"  WHERE "users"."email" = '[email protected]' LIMIT 1
   (0.2ms)  ROLLBACK

User.all :

  User Load (1.0ms)  SELECT "users".* FROM "users"
  User Load (1.0ms)  SELECT "users".* FROM "users"
=> []

Upvotes: 1

Views: 342

Answers (2)

Tonmoy
Tonmoy

Reputation: 174

Either use User.create! or code like this

user = User.create(first_name: 'John', last_name: 'Doe', email: '[email protected]', password: 'testing', is_admin: true)

then run user.save

Upvotes: 0

Kirti Thorat
Kirti Thorat

Reputation: 53038

It looks like the user creation is failing because of some validation error i.e., any of the validations on the User model are prohibiting the creation of your user record.

What you can do is , update the seeds.rb with:

User.create!(first_name: 'John', last_name: 'Doe', email: '[email protected]', password: 'testing', is_admin: true) 

Notice create! instead of create.

This way you would know on which validation the user creation failed.

Upvotes: 2

Related Questions