Reputation: 1274
I'm having User model (within PostgreSQL database) in my RoR application that's using Devise for authentication. Now I want users to be invited, so I decided to use devise_invitable gem. Following the installation guide I did:
Than I tried to run the migration, generated above (it is the default one given by this gem and it looks like:)
class DeviseInvitableAddToUsers < ActiveRecord::Migration
def up
change_table :users do |t|
t.string :invitation_token, :limit => 60
t.datetime :invitation_sent_at
t.datetime :invitation_accepted_at
t.integer :invitation_limit
t.references :invited_by, :polymorphic => true
t.index :invitation_token, :unique => true # for invitable
t.index :invited_by_id
end
# And allow null encrypted_password and password_salt:
change_column_null :users, :encrypted_password, true
end
def down
change_table :users do |t|
t.remove_references :invited_by, :polymorphic => true
t.remove :invitation_limit, :invitation_sent_at, :invitation_accepted_at, :invitation_token
end
end
end
But, while running migrations, I get:
== DeviseInvitableAddToUsers: migrating ======================================
-- change_table(:users)
rake aborted!
An error has occurred, this and all later migrations canceled:
PG::Error: ERROR: relation "invited_bies" does not exist
: ALTER TABLE "users" ADD CONSTRAINT fk_users_invited_by_id FOREIGN KEY ("invited_by_id") REFERENCES "invited_bies" ("id")
I am not guru in PostgreSQL and it's foreign keys, but it looks like there should be invited_bies table, should not it? But it is not being created. So, I'm a bit puzzled with this all.
My User model for devise:
devise :invitable, :database_authenticatable, :recoverable, :rememberable, token_authenticatable,:trackable, :authentication_keys => [:email]
The fields :invitable, :database_authenticatable
were added by the generating script.
Upvotes: 0
Views: 1636
Reputation: 1174
Are you using Schema_plus Gem?
If you are using it, this Gem will try to create a foreign key relationship based on the _id for any field.
You need to disable this for field like this
t.integer :invited_by_id, foreign_key: false
Let me know if this is the case.
Upvotes: 2
Reputation: 3484
Your problem is here:
3. Run rails generate devise_invitable
That should be
rails generate devise_invitable MODEL
and you replace MODEL with the name of your devise model. That is probably User, but it should be the name of the model that you are having devise manage for you. So if you use User as your model:
rails generate devise_invitable User
Because you did not provide a model name, the generator is generating invalid migrations, which is probably a bug, and you might want to report that). when your migration tries to create the foreign key relationship between the invitation, and the "user" who made the invite "invited_by", it fails because it has no idea which table to reference.
Edit: 2013-05-04:
Does your User model also contain this:
has_many :invitations, :class_name => self.to_s, :as => :invited_by
Upvotes: 2