开发者

Quickly Updating a Bit Field for Added Permissions

I have a Ruby on Rails application that is using bit fields to store user settings. I'm now addi开发者_如何学Gong additional bits that must have the default set based on the users existing permissions. For example, if a user has permission 'A' or 'B', they automatically get permission 'C'. Does any good way exist of doing this through an UPDATE ALL query?


Straight SQL in your migration is probably your best bet:

def self.up
    execute(%Q{
        UPDATE table
        SET permissions = permissions | bit_C
        WHERE permissions & bits_A_and_B <> 0
    })
end

Replace bit_C with the appropriate bitmap that just has the bit for permission C set and bits_A_and_B with the bitmap that just has the bits for permissions A and B set (both of them). The bit operators for PostgreSQL are pretty much the same (except bitwise XOR) as they are in C. Similar things will work if you're using bitstrings instead of integers for your bitmaps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜