我正在使用“flag_shih_tzu”gem,我想知道它可以处理的最大标志数量是多少,或者它取决于 int。标志列中的长度?
我需要它来处理 64 个标志。
它可以?
问问题
723 次
1 回答
7
我是 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 :
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
现在是巨大的警告!如果你想一起使用这 4 列,它们需要位于 SQL WHERE 子句的不同部分,因此处于不同的活动记录关系中。
重要链接标志只能与来自同一列的标志链接。
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 个标志,并将我的标志分成我需要的尽可能多的列。
建议:组合将在同一列上一起查询的属性的标志,以充分利用按位算术。
于 2013-09-03T15:38:39.967 回答