2013-04-08 49 views
2

我正在開發使用設計和Rolify一個Rails 3.2.13應用程序,我需要有3種用戶類型如下:Rails的STI與裝置和後rolify錯誤

class User < ActiveRecord::Base 
    rolify 
    ... 
end 

class UserTypeOne < User 
    .... 
end 

class UserTypeTwo < User 
    .... 
end 

class UserTypeThree < User 
    .... 
end 

當我嘗試種子我的分貝闕創建用戶的工程確定,但它給試圖將一個角色添加到其中任何一個當錯誤:

user = UserTypeOne.find_or_create_by_email :name => 'User one', :email => '[email protected]', :password => 'userone', :password_confirmation => 'userone' 
user.confirm! 
user.add_role :admin 

rake aborted! 
undefined method `find_or_create_by' for nil:NilClass 

但用戶正確插入......我在做什麼錯?

在此先感謝!

+0

我遇到了同樣的問題。你找到解決方案嗎? – jacobsimeon 2013-06-11 20:26:54

+0

@Jacob我沒有。我終於從這個項目中刪除了Rolify,因爲我可以通過STI自己實現我需要的基本角色功能。抱歉。 – bigardone 2013-06-13 06:31:42

回答

0

嘗試增加

self.adapter = User.adapter 

向每個繼承類。

例子:

class UserTypeOne < User 
    self.adapter = User.adapter 
    .... 
end 

這爲我工作。

0

在ActiveRecord中使用STI時,Rolify只知道在當前類 而不是父類中查找。

這裏是問題: https://github.com/EppO/rolify/issues/138

Ethans回答上面應該工作,但Here是在Rolify常見問題的答案。

就我個人而言,我不喜歡這兩種解決方案之一。 也許Rolify可以在繼承類時傳遞適配器。例如:

def self.inherited(klass) 
    klass.adapter = self.adapter 
    super # EDIT: added super call to prevent ActiveRecord error (NoMethodError: undefined method `synchronize' for nil:NilClass) 
end