0

When a user creates an accounts, the code should allow that user to register a user account through Devise and then create an account, plus a company.

The code should redirect to a wizard setup using the wicked gem. Users have roles (rolify) and are authorized with Cancancan.

The tables are setup using has_many :through association. This seems to work.

The models are as follows:

user
   has_many :accounts_users
   has_many :accounts, through: :accounts_users, dependent: :destroy
   has_one :company, through: :accounts, dependent: :destroy


users_account
    belongs_to :account
    belongs_to :user

account
    resourcify
    has_many :accounts_users
    has_many :users, through: :accounts_users, dependent: :destroy
    has_one :company

company
    belongs_to :account

The create account controller is the following:

  def new
    @account = Account.new
  end

def create
    if !current_user.present?
      build_resource(sign_in_params)
      account = Account.find_or_create_by(org_name: params[:user]    [:account])
    else
      @account = current_user.accounts.create!(account_params)
    end

# create a default account and company
    @account.save!
    current_user.add_role 'owner'
    current_account = current_user.accounts.first
# create a company associated with the newly created account
    if current_account.companies.empty?
      company = current_account.companies.create(company_name: params[:org_name])
    else
      company = current_account.companies.first
    end
    resource.update(current_company: company.id)
    respond_to do |format|
      if @account.save
 # redirect to the relevant wizard
        format.html { redirect_to after_account_path(:add_account),     notice: 'Account was successfully created.' }
      else
        format.html { render action: 'new' }
        format.json { render json: @account.errors, status: :unprocessable_entity }
      end
    end
  end

EDITED VERSION

  def create
    @account = Account.create(account_params.except(:company))
    @account.save!
    respond_to do |format|
      if @account.save
        create_company
        format.html { redirect_to @account, notice: 'Account was successfully created.' }
        format.json { render :show, status: :created, location: @account }
      else
        format.html { render :new }
        format.json { render json: @account.errors, status: :unprocessable_entity }
      end
    end
  end

  def create_company
    current_user.add_role 'owner'
    current_account = current_user.accounts.first
    if current_account.company.nil?
      current_account.build_company(company_name: params[:org_name])
      @company.save!
    else
      company = current_account.company
    end
    resource.update(current_company: company.id)
  end

included in Application helper:

  def current_account
    current_user.accounts.first
  end

  def current_company
    current_user.accounts.first.companies.first
  end

it redirects (unexpected) to displaying (show) the Company data right after creation, I receive a nil / no method error.

the index and how controller are:

before_action :set_company, only: [:show, :edit, :update, :destroy,

:new]

  def index; end

  def show
    @company = Company.find_by(id: params[:id])
  end


  def set_company
    @company = current_account.company
  end 

This seems to work. Editing the Company is a challenge but should get there.

Any advices to get to a better code is welcome

Thierry
  • 111
  • 2
  • 14
  • 1
    I think you are making this unnecessary complex. Can a user have more then 1 account? Your AccountsController create action has way to many if else statements. Before the line saying `@account.save` you have an if else statement, if the if statement is true, you are not setting @account so you can't even save it... – Hackman Oct 03 '19 at 18:23
  • Ok. I am reworking it for simplicity. However : I need that the account has many users but only one company. The user can have many accounts. – Thierry Oct 09 '19 at 09:31

1 Answers1

0

Have you thought about just making the front end handle the case where #name isn't set yet?

<=% @company&.name %>

or

<=% @company.name || "Unknown" %>
sevensidedmarble
  • 643
  • 1
  • 4
  • 14