简体   繁体   English

Rails设计了gem和用户组

[英]Rails devise gem and user groups

I'm using rails devise gem for users authentification, but now for my shop i must introduce user groups for discont's, special prices, etc. How to do this with devise? 我正在使用rails devise gem进行用户身份验证,但现在我的商店必须介绍用户组,以获取discont,特价等。如何使用devise进行此操作? Note that this is one/many-to-many, becouse every user can have many groups, and every group some users. 请注意,这是一个/多对多,因为每个用户可以拥有多个组,每个组都有一些用户。

Also when user is registering i't group must be for example 1. 此外,当用户注册时,组必须是例如1。

Devise has "closed" controller (not as in authlogic). Devise已经“关闭”了控制器(不像authlogic那样)。 That is trouble also. 那也是麻烦。

def create
    super
    group = Group.find_by_name("newuser")
    user_group = UserGroup.create
    user_group.user_id = current_user.id
    user_group.group_id = group.group_id
    user_group.save
  end

This doesn't necessarily have to be integrated with Devise unless I'm reading your question wrong. 除非我错误地阅读您的问题,否则不一定必须与Devise集成。 Just create a Group model describing the attributes of a group, and a UserGroup join model: 只需创建一个描述组属性的Group模型,以及UserGroup连接模型:

class UserGroup < ActiveRecord::Base
  belongs_to :user
  belongs_to :group
end

class User < ActiveRecord::Base
  has_many :user_groups
  has_many :groups, :through => :user_groups

  # attr_accessible :user_id, :group_id
end

class Group < ActiveRecord::Base
  has_many :user_groups
  has_many :users, :through => :user_groups
end

As for the closed controller problem, you can lift the Devise controller into your application, or create a new controller which inherits from it and thus override the methods. 至于关闭的控制器问题,您可以将Devise控制器提升到您的应用程序中,或者创建一个继承它的新控制器,从而覆盖这些方法。 Read more from their link GitHub page here . 阅读他们的链接GitHub页面

Edit: I think you are approaching this from the wrong angle. 编辑:我认为你是从错误的角度接近这个。 You needn't do anything from within Devise's controllers, but rather add a before_save callback to your User model. 你不需要在Devise的控制器中做任何事情,而是在你的User模型中添加一个before_save回调。

class User < ActiveRecord::Base
  before_save(:on => :create) :assign_default_group

  # Other model stuff here

  private

  def assign_default_group
    # This automatically creates the UserGroup record
    self.groups << Group.find_by_name("User")
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM