Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 鸭翼宝石-我应该如何编程使管理员用户?_Ruby On Rails_Ruby_Activerecord_Cancan - Fatal编程技术网

Ruby on rails 鸭翼宝石-我应该如何编程使管理员用户?

Ruby on rails 鸭翼宝石-我应该如何编程使管理员用户?,ruby-on-rails,ruby,activerecord,cancan,Ruby On Rails,Ruby,Activerecord,Cancan,我在RubyonRails上使用(CanCan+RoleModel)进行身份验证,但在创建初始用户管理员时遇到了一些问题。帐户有用户。创建帐户时,有一个拥有者用户,他有账单详细信息。我希望该所有者具有:admin角色 class SomewhereElseInSystem ... account = Account.register(hash) # Company Admin submits form with # d

我在RubyonRails上使用(CanCan+RoleModel)进行身份验证,但在创建初始用户管理员时遇到了一些问题。帐户有用户。创建帐户时,有一个拥有者用户,他有账单详细信息。我希望该所有者具有:admin角色

class SomewhereElseInSystem
  ...
  account = Account.register(hash) # Company Admin submits form with
                                   # details of account, owning user,
                                   # and a load more objects we don't
                                   # show here.
end

class Account < ActiveRecord::Base
  has_many :users, dependent: :destroy, include: [:profile]
  has_one :owner, class_name: 'User', conditions: { owner: true }
  accepts_nested_attributes_for :owner

  def self.register(details)
    ...
    hash = { name:   details[:client_name],
             domain: details[:subdomain],
             plan:   details[:plan],
             owner_attributes: {
               name:                  details[:owner_name],
               password:              details[:owner_password],
               password_confirmation: details[:owner_password],
               email:                 details[:owner_email] } } 
    account = Account.create hash
    ...
  end
end

class User < ActiveRecord::Base
  belongs_to :account
  acts_as_user roles: [:saas_admin, :admin, :manager]

  # owner is a boolean DB field

  before_validation :owners_are_admins

  protected

  def owners_are_admins
    return unless owner
    self.roles << :admin
  end
end
但如果我这样做,那就更好了

account.users.admins # 0
owner = account.owner
owner.roles << :admin # Should be a no-op
owner.save!
account.user.admins # 1!
account.users.admins\0
owner=account.owner

owner.roles您可以共享用于创建帐户和owner user的代码吗?上面更新的代码显示“注册”呼叫。
account.users.admins # 0
owner = account.owner
owner.roles << :admin # Should be a no-op
owner.save!
account.user.admins # 1!