2015-11-17 77 views
-2

我想能夠使用toggle,但不太清楚如何做到這一點。如何使用Rails切換?

我在Member模型上有一個布爾屬性deceased

但是,當我嘗試切換它與toggle(attribute)它不起作用。

這是在命令行發生了什麼:

[41] pry(main)> member.deceased.toggle! 
NoMethodError: undefined method `toggle!' for true:TrueClass 
from (pry):41:in `__pry__' 
[42] pry(main)> member.toggle(deceased) 
NameError: undefined local variable or method `deceased' for main:Object 
from (pry):42:in `__pry__' 
[43] pry(main)> member.deceased.toggle 
NoMethodError: undefined method `toggle' for true:TrueClass 
from (pry):43:in `__pry__' 
[44] pry(main)> member.deceased 
=> true 
[45] pry(main)> toggle(member.deceased) 
NoMethodError: undefined method `toggle' for main:Object 
from (pry):45:in `__pry__' 

如何使用toggle

供參考:以上所有都在rails c ......但我已經安裝pry。但鑑於toggle似乎是一個ActiveRecord方法,它不應該是一個問題嗎?

回答

3

你應該能夠

member.toggle(:deceased) 
+0

這就是答案 –

+0

完美的做到這一點。非常感謝! – marcamillion