chrickso
chrickso

Reputation: 3034

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

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.

Upvotes: 1

Views: 156

Answers (1)

Ashik Salman
Ashik Salman

Reputation: 1879

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 !

Upvotes: 3

Related Questions