سؤال

Question: How can I achieve the model design below using ActiveAdmin and Devise 2?

I have set up active_admin successfully with an existing User model for bootstrapping it.

I assumed this model design means ("Have adminusers manage users").

Here is my current model set up:

 irb(main):003:0> User.column_names
 => ["id", "created_at", "updated_at", "avatar", "name"]
 irb(main):004:0> AdminUser.column_names
 => ["id", "email", "encrypted_password", "reset_password_token",    "reset_password_sent_at", "remember_created_at", "sign_in_count", "current_sign_in_at", "last_sign_in_at", "current_sign_in_ip", "last_sign_in_ip", "created_at", "updated_at"]

Now I would like to add some authenticaton for my User model. However I am unable to migrate the results of rails generate devise User without this conflict:

 ==  AddDeviseToUsers: migrating ===============================================
 -- change_table(:users)
 -> 0.7201s
 -- add_index(:users, :email, {:unique=>true})
rake aborted!
An error has occurred, this and all later migrations canceled:

PG::Error: ERROR:  could not create unique index "index_users_on_email"
DETAIL:  Key (email)=() is duplicated.
: CREATE UNIQUE INDEX "index_users_on_email" ON "users" ("email")

Tasks: TOP => db:migrate

Now I do not have an email attribute in my current User model, but AdminUser does. So when Devise tries to create the email attribute in User I suspect this is why I get this error. But why? They are in different models?

Any help e.g. experiences, posts or tutorials would be appreciated (as well as an answer)

NOTE I have tried the solution found on the Devise wiki and here, with no success on migration.

هل كانت مفيدة؟

المحلول

The problem is in the creation of the index. Since you are changing the table, I'm assuming you already have records in it - trying to add an email column and subsequently add a unique index on it will not work because the column won't have unique values yet. Devise adds an index on the email column because it is the primary login field - if you have a different field for login (say, username), then add the index on that instead. If you are planning to start using email for login, then remove the index for now and add it once you have populated the email column with unique values.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top