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.