【问题标题】:Adding devise invitable not working添加设计邀请不起作用
【发布时间】:2013-05-03 12:59:48
【问题描述】:

我在使用 Devise 进行身份验证的 RoR 应用程序中有用户模型(在 PostgreSQL 数据库中)。现在我想邀请用户,所以我决定使用devise_invitable gem。按照我所做的安装指南:

  1. 将 gem 'devise_invitable' 添加到我的 Gemfile 中
  2. 运行 rails generate devise_invitable:install
  3. 运行 rails 生成 devise_invitable 用户

比我尝试运行上面生成的迁移(它是这个 gem 给出的默认值,它看起来像:)

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

但是,在运行迁移时,我得到:

==  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")

我不是 PostgreSQL 中的大师,它是外键,但看起来应该有被邀请的_bies 表,不是吗?但它不是被创造出来的。所以,我对这一切有点困惑。

我的用户模型:

devise :invitable, :database_authenticatable, :recoverable, :rememberable, token_authenticatable,:trackable, :authentication_keys => [:email]

:invitable, :database_authenticatable 字段是由生成脚本添加的。

【问题讨论】:

    标签: ruby-on-rails devise devise-invitable


    【解决方案1】:

    您在使用 Schema_plus Gem 吗?

    如果你正在使用它,这个 Gem 将尝试根据任何字段的 _id 创建外键关系。

    您需要为这样的字段禁用此功能

    t.integer  :invited_by_id,  foreign_key: false
    

    如果是这种情况,请告诉我。

    【讨论】:

    • 谢谢!这是 100% 正确的。我在遗留项目中,错过了这个 evil 模式加上 gem。因此,除了您提出的代码之外,我还必须添加:t.references :invited_by, :polymorphic =&gt; true, foreign_key: false,因为它(schema_plus gem)也想为“参考”字段创建 id。
    【解决方案2】:

    你的问题在这里:

    3. Run rails generate devise_invitable
    

    应该是这样的

    rails generate devise_invitable MODEL
    

    然后您将 MODEL 替换为您的设计模型的名称。那可能是用户,但它应该是您正在设计为您管理的模型的名称。因此,如果您使用 User 作为模型:

    rails generate devise_invitable User
    

    因为您没有提供模型名称,所以生成器正在生成无效迁移,这可能是一个错误,您可能需要报告该问题)。当您的迁移尝试在邀请和发出邀请“invited_by”的“用户”之间创建外键关系时,它会失败,因为它不知道要引用哪个表。

    编辑:2013-05-04:

    你的用户模型是否也包含这个:

     has_many :invitations, :class_name => self.to_s, :as => :invited_by
    

    【讨论】:

    • 对不起,我可能没有提到它,但我确实使用 User 模型进行迁移,因此您可以看到 :invitable 已添加到我的 User 模型中。
    • 好吧。直到今晚我可以回家并将我的设置与您的设置进行比较时,我才能提供任何进一步的帮助。
    • 我将不胜感激!顺便说一句,我通过删除所有处理受邀列的东西来使它工作(迁移),但这当然不是解决方案。
    • 没问题。我要么在工作和糟糕的电影之夜之间有时间,要么我早上去。
    • 感谢您的帮助,不胜感激。但我的问题是 schema_plus gem,我错过了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2016-11-18
    • 2011-04-11
    • 1970-01-01
    相关资源
    最近更新 更多