2013-08-07 44 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個標誌,並將我的標誌分成我需要的列數。

推薦:將在同一列上一起查詢的屬性組合標誌,以充分利用按位算術。

相關問題