2013-08-07 42 views
4

我使用“flag_shih_tzu”宝石,我想知道它可以处理的最大标志量是多少,还是取决于int。标志列中的长度?
我需要它来处理64个标志。
可以吗?flag_shih_tzu可以处理的最大标志量是多少?

+2

这个问题似乎是题外话,因为它应该在GitHub页面上询问该项目(https://github.com/pboling/flag_shih_tzu) –

+3

@Ryan Bigg:是否有关于在StackOverflow上与gem无关的问题?不是Rails的宝石? –

回答

6

我是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.new 
foo.is_d16 = false 
foo.save 

现在你可以检索FOO这样的:


Foo.not_is_d16 # => [foo] 

如果你想在同一个查询中检查其他标志,你应该把条件连在一起(在一个按位优化的mann中呃)如下:


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个标志,并将我的标志分成我需要的列数。

推荐:将在同一列上一起查询的属性组合标志,以充分利用按位算术。

相关问题