所以出于某种原因,我们的数据库人员决定使用 mysql 数据类型集。我正在尝试使其与导轨一起使用。这是我们的专栏:
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
我不知道为什么。