Mysql datatype SET with rails
So for some reason, our database guy decided to use the mysql datatype
set. I'm trying to make it work with rails. Here is our column:
actions => set('spoke_to','left_voicemail','emailed')
To make it work I have the following code which i'm sure could be far better:
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
However, setting the action(s) works just like fine, but when I try to
unset one I have the following weird query being generated:
(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
And I have no idea why.
No comments:
Post a Comment