2012-03-05 96 views

回答

2

AR::Callbacks爲你做的伎倆,但我更喜歡建立角色時,用戶對象首先被初始化:

class Role < ActiveRecord::Base 
end 

class User < ActiveRecord::Base 
    after_initialize :set_default_roles 

    private 
    def set_default_roles 
     self.roles = self.roles || [Role.find_by_name('Default Role')] 
    end 
end