使用Rails 5,如何在创建新用户后添加数据库记录?

时间:2018-04-02 04:44:45

标签: ruby-on-rails activerecord

在我的应用程序中,我有帐户,用户和权限。创建新用户时,我已经自动创建了一个帐户记录,新的用户foreign_key被设置为" owner_id"在帐户表中。在完成此操作的同时,我想在我的Permissions表中添加一条记录,其中新的account_id和新的user_id设置在各自的列中,以及资源列设置为" Account"并将角色列设置为"所有者"。

以下是我的模型的外观:

account.rb

class Account < ApplicationRecord
  belongs_to :owner, class_name: "User"
  has_many :permissions
end

user.rb

class User < ApplicationRecord  

  ...

  has_one :account, foreign_key: "owner_id"
  has_many :permissions

  after_initialize :set_account

  ...

  private

    def set_account
      build_account unless account.present?
    end

end

permissions.rb

class Permission < ApplicationRecord
  belongs_to :account
  belongs_to :user
end

我希望通过修改&#34; set_account&#34;以下内容至少会填充Permissions表的account_id和user_id列,但我得到一个&#34;未定义的局部变量或方法&#39; build_permission&#39;错误。

def set_account
  build_account and build_permission unless account.present?
end

这是我的模式文件,用于显示表/列:

ActiveRecord::Schema.define(version: 2018_04_02_040606) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "accounts", force: :cascade do |t|
    t.integer "hash_id"
    t.integer "owner_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["hash_id"], name: "index_accounts_on_hash_id", unique: true
    t.index ["owner_id"], name: "index_accounts_on_owner_id"
  end

  create_table "permissions", force: :cascade do |t|
    t.integer "account_id"
    t.integer "user_id"
    t.string "resource"
    t.string "role"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["account_id", "user_id"], name: "index_permissions_on_account_id_and_user_id", unique: true
  end

  create_table "users", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer "sign_in_count", default: 0, null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.inet "current_sign_in_ip"
    t.inet "last_sign_in_ip"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.jsonb "settings"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

  add_foreign_key "users"
end

我很欣赏任何有关我可能遗失的信息,或者我从哪里开始,让它按照我的意愿运作。

1 个答案:

答案 0 :(得分:3)

除非创建了帐户对象,否则无法让account_id在权限表中更新它。一种选择是通过after_create回拨来设置帐户和权限。

class User < ApplicationRecord  

  ...

  has_one :account, foreign_key: "owner_id"
  has_many :permissions

  after_create :set_account_and_permission

  ...

  private

  def set_account_and_permission
    account = create_account
    permissions.create(role: 'owner', account_id: account.id, resource: 'Account')
  end

end

希望它有所帮助!