简体   繁体   中英

Create record for another model when account is created?

I have a multi-tenant application. When an account is created, the account belongs to an owner and also creates a user record for the owner. This owner can invite other users through a memberships join table. All users except for account owners have memberships to the account.

For users with memberships (not owners of accounts) the account/users/:id show page shows up. I would like the same for account owners, but am receiving the following error message:

ActiveRecord::RecordNotFound in Accounts::UsersController#show
Couldn't find User with 'id'=2 [WHERE "memberships"."account_id" = $1]

def show
  @user = current_account.users.find(params[:id])
end

I can add a membership to the owner user in the admin panel and this error goes away, however I would like to add the membership to the owner/user when they create their account.

Any ideas?

Adding @account.memberships.build(user_id: current_user, account_id: current_account) before if @account.save in the accounts controller below does not seem to work.

controllers

user.rb

module Accounts
  class UsersController < Accounts::BaseController
    before_action :authorize_owner!, only: [:edit, :show, :update, :destroy]

    def show
      @user = current_account.users.find(params[:id])
    end

    def destroy
      user = User.find(params[:id])
      current_account.users.delete(user)
      flash[:notice] = "#{user.email} has been removed from this account."
      redirect_to users_path
    end
  end
end

accounts_controller.rb

class AccountsController < ApplicationController
  def new
    @account = Account.new
    @account.build_owner
  end

  def create
    @account = Account.new(account_params)
    if @account.save
      sign_in(@account.owner)
      flash[:notice] = "Your account has been created."
      redirect_to root_url(subdomain: @account.subdomain)
    else
      flash.now[:alert] = "Sorry, your account could not be created."
      render :new
    end
  end

  private

    def account_params
      params.require(:account).permit(:name, :subdomain,
        { owner_attributes: [:email, :password, :password_confirmation
        ]}
      )
    end
end

Models

user.rb

class User < ApplicationRecord
  has_many :memberships
  has_many :accounts, through: :memberships

  def owned_accounts
    Account.where(owner: self)
  end

  def all_accounts
    owned_accounts + accounts
  end
end

account.rb

class Account < ApplicationRecord
  belongs_to :owner, class_name: "User"
  accepts_nested_attributes_for :owner

  validates :subdomain, presence: true, uniqueness: true

  has_many :memberships
  has_many :users, through: :memberships
end

membership.rb

class Membership < ApplicationRecord
  belongs_to :account
  belongs_to :user
end

Have you tried callback after_create ? If it works, you will need to figure it on client and admin create an account, self assigning (admin) against on create assigning (client).

# models/account.rb
  after_create do
    self.memberships.create(user_id: self.owner, account_id: self.id)
  end

Ended up answering this question by putting this line under if @account.save :

if @account.save
  @account.memberships.create(user_id: @account.owner.id, account_id: @account.id)

Probably not ideal, but it works for now. I might end up making a service or something like that for it, though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM