With Rails 5, how do I add a database record after a new User has been created?

馋奶兔 提交于 2019-12-14 02:23:13

问题


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".

Here is how my models look:

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

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.


回答1:


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

Hope it helps !



来源:https://stackoverflow.com/questions/49605418/with-rails-5-how-do-i-add-a-database-record-after-a-new-user-has-been-created

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!