2014-11-06 64 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