通过 Devise_Invitable 发送邀请时向用户添加属性
Adding Attributes to User when invitation is sent via Devise_Invitable
我很困惑,不确定如何在通过 devise_invitble 创建用户帐户时将 "account_id" 添加到用户(发送初始邀请时)。
基本工作流程是所有者创建一个帐户,然后该帐户的所有者可以邀请某人使用该应用程序并设计邀请。我需要跟踪用户是否与该帐户相关联,因为根据计划类型,一个帐户只有 "x" 个用户。
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
这就是我现在正在使用的,但是当我在 rails 控制台中拉出该用户并在服务器输出中查看它时,用户 account_id 仍然为零。
这是帐号型号:
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
这是用户模型:
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
拜托,如有任何帮助,我们将不胜感激!这真的让我很烦。
@user.update_attributes(:account_id => current_account.id )
失败,因为对用户模型的验证未通过。解决此问题的一种方法是使用 update_all 更新用户记录,这是一种仅 SQL 的方法,将绕过验证:
def update_user_account
User.where(email: params[:user][:email]).update_all(account_id: current_account.id)
end
或更少代码:
def create
super
User.where(email: params[:user][:email]).update_all(account_id: current_user.account.id)
end
我很困惑,不确定如何在通过 devise_invitble 创建用户帐户时将 "account_id" 添加到用户(发送初始邀请时)。
基本工作流程是所有者创建一个帐户,然后该帐户的所有者可以邀请某人使用该应用程序并设计邀请。我需要跟踪用户是否与该帐户相关联,因为根据计划类型,一个帐户只有 "x" 个用户。
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
这就是我现在正在使用的,但是当我在 rails 控制台中拉出该用户并在服务器输出中查看它时,用户 account_id 仍然为零。
这是帐号型号:
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
这是用户模型:
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
拜托,如有任何帮助,我们将不胜感激!这真的让我很烦。
@user.update_attributes(:account_id => current_account.id )
失败,因为对用户模型的验证未通过。解决此问题的一种方法是使用 update_all 更新用户记录,这是一种仅 SQL 的方法,将绕过验证:
def update_user_account
User.where(email: params[:user][:email]).update_all(account_id: current_account.id)
end
或更少代码:
def create
super
User.where(email: params[:user][:email]).update_all(account_id: current_user.account.id)
end