在我的模型示例游戏中,有一个状态列。但我通常通过使用符号来设置状态。例子
self.status = :active
MATCH_STATUS = {
:betting_on => "Betting is on",
:home_team_won => "Home team has won",
:visiting_team_won => "Visiting team has one",
:game_tie => "Game is tied"
}.freeze
def viewable_status
MATCH_STATUS[self.status]
end
我使用上面的地图在可见状态之间切换,反之亦然。
但是,当数据保存到数据库时,ActiveRecord 会在每个状态后附加“---”。所以当我检索回来时,状态就被搞砸了。
正确的做法应该是什么?
重写 getter 和 setter:
def status
read_attribute(:status).to_sym
end
def status=(new_status)
write_attribute :status, new_status.to_s
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)