1

Im am stuck and unsure how to add "account_id" to a user when their account is created via devise_invitble (When the initial invitation is sent).

The basic workflow is an owner creates an account, then the owner of the account can invite someone to use the app with devise invitable. I need to track that the user is associated to the account, as an account only has "x" number of users based on the plan type.

class InvitationsController < Devise::InvitationsController

  after_action :update_user_account, :only => [:create]


  def update_user_account
    @user = User.find_by_email(params[:user][:email])
    @user.update_attributes(:account_id => current_account.id )
  end

end

this is what I am using right now, however when I pull that user up in rails console and look at it in the server output, the users account_id is still nil.

this is the account Model:

class Account < ApplicationRecord
  include ImageUploader[:image]
  # Constants
  RESTRICTED_SUBDOMAINS = %w(www patrolvault admin test type taurenapplabs taurenmaterialservices)

  # Before Actions
  before_validation :downcase_subdomain

  # Relationships
  belongs_to :owner, class_name: 'User', optional: true
  accepts_nested_attributes_for :owner
  has_many :users

  # Validations
  validates :owner, presence: true

  validates :subdomain, presence: true,
                        uniqueness: { case_sensitive: false },
                        format: { with: /\A[\w\-]+\Z/i, message: 'Contains invalid characters.' },
                        exclusion: { in: RESTRICTED_SUBDOMAINS, message: 'Restricted domain name'}

  has_one :plan
  accepts_nested_attributes_for :plan

  private

  def downcase_subdomain
    self.subdomain = self.subdomain.downcase
  end

end

This is the User Model:

class User < ApplicationRecord
  # Constants & Enums
  USER_LIMITS = ActiveSupport::HashWithIndifferentAccess.new(
    #Plan Name      #Auth Users
    responder:        6,
    first_responder:  12,
    patrol_pro:       30,
    guardian:         60
  )

  # Before Actions

  # Devise Modules
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable, :invitable, :lockable, :timeoutable

  # Relationships
  belongs_to :account, optional: true

  # Validations
  validates :f_name, presence: true
  validates :l_name, presence: true
  validates :date_of_birth, presence: true

  #validate :must_be_below_user_limit

  # Custom Methods
  def full_name
    l_name.upcase + ", " + f_name
  end

end

Please, Any assistance here would be greatly appreciated!, this is really jamming me up.

Shawn Wilson
  • 1,180
  • 10
  • 27
  • It sounds like the user record may not be saving because the validations are not passing. Are you sure the user has an f_name, l_name and date_of_birth? – infused Oct 19 '16 at 20:29
  • @infused when i send the invite, the only filled out fields in the console is email and all the invitable fields, When the user accepts the invitation, they are presented with a field that allows them to add their name and password, all of this passes and is present in the console and log... the only item that dose not update is the account_id. -- ill add the console readout above. – Shawn Wilson Oct 19 '16 at 20:38
  • It's because `@user.update_attributes(:account_id => current_account.id )` fails because of the validation errors. – infused Oct 19 '16 at 20:39
  • @infused, how can i make this pass then? or just remove the validations? – Shawn Wilson Oct 19 '16 at 20:42

2 Answers2

3

@user.update_attributes(:account_id => current_account.id ) is failing because the validations on the User model are not passing. One way to fix this is to update the user record using update_all, which is a SQL only method that will bypass the validations:

def update_user_account
  User.where(email: params[:user][:email]).update_all(account_id: current_account.id)
end
infused
  • 22,433
  • 13
  • 63
  • 74
1

Or less code:

def create
  super
  User.where(email: params[:user][:email]).update_all(account_id: current_user.account.id)
end
Floern
  • 31,495
  • 23
  • 98
  • 115
127
  • 113
  • 1
  • 7