Re:100 more of those BITFIELDs
by Zikes on Friday February 26, 2016 @11:19AM
(#11182170)
That sounds pretty painful to query, though. If the first bit position represented if the user had confirmed their email address, wouldn't a SELECT for confirmed users look something like SELECT * FROM users WHERE (mydata & b'0000000000000000000000000000000000000000000000000000000000000001')?
[ Parent
| on HN ]
No, you can just compare against an integer: SELECT * FROM users WHERE mysql & 1
This sounds like it'd be a full table scan to me.
Re:100 more of those BITFIELDs
by andrewfromx on Friday February 26, 2016 @11:21AM
(#11182193)
in rails I add:
[:foo_boolean1, :foo_boolean2].each_with_index do |flavor, i|
power = 1
i.times do
power *= 2
end
define_method "has_#{flavor}" do
(foo_options & power) > 0
end
define_method "has_#{flavor}=" do |value|
value = value == '1' if value.class == String
current_value = self.send("has_#{flavor}")
if (current_value && !value) || (!current_value && value)
self.foo_options = foo_options ^ power
end
end
end
which lets me access each col like normal. blah.has_foo_boolean1 or blah.has_foo_boolean1=true etc.You might want to take a look at https://github.com/pboling/flag_shih_tzu, which does all of that and a lot more, like adding database scopes.
Re:100 more of those BITFIELDs
by andrewfromx on Friday February 26, 2016 @12:18PM
(#11182578)
reminds me of this http://jeremykun.com/2013/02/08/why-there-is-no-hitchhikers-... article about doing Math cuz u love it.
Re:100 more of those BITFIELDs
by andrewfromx on Friday February 26, 2016 @12:17PM
(#11182573)
aw, but it was so much fun writing that logic myself and really learning how bit ops work in ruby.