因此,出于某种原因,我们的数据库人决定使用mysql数据类型集。 我正在尝试使用rails。 这是我们的专栏:
actions => set('spoke_to','left_voicemail','emailed')
为了使它工作,我有以下代码,我相信可能会好得多:
def actions=(type)
write_attribute(:actions, pack(type))
end
def actions
read_attribute(:actions).split(",") unless read_attribute(:actions).nil?
end
private
def pack(type)
acs = self.actions
if acs.present? && acs.include?(type)
acs.delete(type)
delete_actions(acs)
else
write_actions(acs, type)
end
end
def delete_actions(acs)
if acs.empty?
write_attribute(:actions, nil)
else
write_attribute(:actions, acs)
end
end
def write_actions(acs, type)
debugger
if acs.present?
write_attribute(:actions, acs.push(type).join(","))
else
write_attribute(:actions, type)
end
end
然而,设置动作效果很好,但是当我尝试取消设置动作时,我生成了以下奇怪的查询:
(72.5ms) BEGIN
(73.6ms) UPDATE `service_requests` SET `actions` = '---\n- spoke_to\n- left_voicemail\n' WHERE `service_requests`.`nid` = 69524843
(78.2ms) COMMIT
我不明白为什么。
答案 0 :(得分:0)
因此,删除方法中的这一行write_attribute(:actions, acs)
发生了错误。
这是因为write_attribute试图在DB中编写yaml(我认为)所以我不得不这样做:
def delete_actions(acs)
if acs.empty?
write_attribute(:actions, nil)
else
write_attribute(:actions, acs.join(','))
end
end
我觉得有点愚蠢而不是马上考虑它,但是,这就是生命......