使用Rails 5后,如何在创建新用户后添加数据库记录? [英] With Rails 5, how do I add a database record after a new User has been created?

查看:94
本文介绍了使用Rails 5后,如何在创建新用户后添加数据库记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我具有帐户,用户和权限.创建新用户时,我已经达到自动创建一个帐户记录的目的,而新用户User_key在Account表中设置为"owner_id".同时完成此操作,我想在我的权限表中添加一条记录,并在各自的列中设置新的account_id和new user_id,将资源列设置为"Account",将角色列设置为"owner" .

In my application, I have Accounts, Users, and Permissions. When a new user is created, I have gotten as far as automatically creating an Account record with the new User foreign_key set as an "owner_id" in the Account table. At the same time this is done, I want to add a record to my Permissions table with the new account_id and new user_id set in their respective columns, as well as the resource column set to "Account" and the role column set to "owner".

这是我的模型的外观:

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

我希望通过将"set_account"修改为以下内容,至少可以填充权限"表的account_id和user_id列,但会收到未定义的局部变量或方法'build_permission'"错误.

I was hoping that by modifying the "set_account" to the following would at least populate the account_id and user_id columns of the Permissions table, but I get an "undefined local variable or method 'build_permission' error.

def set_account
  build_account and build_permission unless account.present?
end

这是我的架构文件,用于显示表/列:

here is my schema file to show tables/columns:

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

对于我可能会缺少的东西或从这里到如何使它按我希望的方式工作的任何见解,我将不胜感激.

I would appreciate any insight on what I might be missing or where I go from here to have it work how I want.

推荐答案

除非创建了帐户对象,否则无法在权限表中获取account_id对其进行更新.一种选择是调用after_create来设置帐户和权限.

Unless account object is created, you can not get account_id to update it in permissions table. One option would be to have an after_create call back to set account and permissions.

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

希望有帮助!

这篇关于使用Rails 5后,如何在创建新用户后添加数据库记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆