我正在使用"flag_shih_tzu"宝石,想知道它能处理的最大标志数是多少,还是取决于标志列中int长度?
我需要它处理64个标志。
可以吗?
我正在使用"flag_shih_tzu"宝石,想知道它能处理的最大标志数是多少,还是取决于标志列中int长度?
我需要它处理64个标志。
可以吗?
我是flag_shih_tzu的维护者。
最佳实践:每个用于标志的列最多应设置16个标志,出于性能考虑。如果一个列保存超过16个标志,则会发现性能下降得太严重。
解决方法:单个表可以具有多个标志列。
我将创建以下设计:
class Foo ...
has_flags 1 => :is_a1,
# ... snip ...
16 => :is_a16,
:column => 'flag_col_a'
has_flags 1 => :is_b1,
# ... snip ...
16 => :is_b16,
:column => 'flag_col_b'
has_flags 1 => :is_c1,
# ... snip ...
16 => :is_c16,
:column => 'flag_col_c'
has_flags 1 => :is_d1,
# ... snip ...
16 => :is_d16,
:column => 'flag_col_d'
end
现在,当您拥有Foo的实例时:
foo = Foo.new
foo.is_d16 = false
foo.save
Foo.not_is_d16 # => [foo]
如果您想在同一查询中检查其他标志,您应该将条件链接在一起(以位优化的方式),如下所示:
Foo.chained_flags_with(:not_is_d16, :is_d1, :is_d4, :not_is_d11, :is_d14) # => array of Foo objects matching the conditions
现在有一个重要的限制!如果你想一起使用这四列,它们需要在SQL WHERE子句的不同部分,并且因此在不同的Active Record关系中。
重要提示 链接标记只能与来自同一列的标记链接。
Foo.
chained_flags_with(:not_is_a1, :is_a2). # from flag_col_a
chained_flags_with(:not_is_b3, :is_b4). # from flag_col_b
chained_flags_with(:not_is_c8, :is_c11). # from flag_col_c
chained_flags_with(:not_is_d13, :is_d14) # from flag_col_d
个人而言,我从不在一列中超过8个标志,并将我的标志分成尽可能多的列。
建议:将将一起查询的属性的标志合并到同一列中,以最大限度地利用位运算。