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

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

以下是我的模型的外观:

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 

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

 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 

我很感激任何关于我可能缺少的东西或者我从哪里开始让它按照我想要的方式工作的见解。

除非创建了帐户对象,否则无法通过权限表中的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 

希望能帮助到你 !

我建议合并帐户和用户模型,因为当您的应用程序变大时,您的代码变得复杂,您可能需要进行非规范化。

对于你的问题,你可以做一些事情;

控制器:

 . . def register User.new(params[:user]).init_account(params[:perms]) end 

模型:

 . . def init_account(perms) account = self.build_account perms.collect!{|p| Permission.new p } self.transaction do account.permissions << perms self.save! end end