2014-11-06 65 views
0

我認爲這可能是在其他地方回答的,但我並不是100%確定要查找什麼來尋找答案。RoR:max_users沒有保存?

我想在我length_of_users方法來設置max_users,但由於某種原因它沒有被保存,然後當length_of_users出現,MAX_USERS被顯示爲nil

任何幫助,非常感謝。提前致謝。

這裏是我的資產模型代碼:

在毫不相關的代碼
validate :length_of_users 
. 
. 
. 
after_initialize :set_max_users 
. 
. 
. 
def set_max_users 
    if max_users.nil? 
    max_users = 1 
    end 
end 
. 
. 
. 
def length_of_users 
    if user_ids.count > max_users 
    errors.add(:users, "You can only add a maximum of #{max_users} users") 
    end 
end 

...都顯示空白。

回答

2

通過max_users = 1您只設置局部變量max_users,這是無用的。您應該有:

self.max_users = 1 

使用max_users= setter方法。

+0

我不能相信我錯過了。非常感謝。 – Briknthewall 2014-11-06 15:43:55