2012-11-12 25 views
0

我有一個公司模型,有許多行業和一個級別。鐵路和solr如何限制或設置可搜索的屬性ID範圍

我有一個幫助功能,可以根據關卡獲取行業數量。

說一個具有「基本」級別的公司模型實例只能在前兩個行業中搜索,儘管它可能包含更多,但我只希望它顯示在前兩個行業的結果中。

在模型中我有

searchable do 
    integer :industry_ids, multiple: true 

如何限制對模型實例的搜索,這樣可以搜索基於由num_of_industries輔助函數確定的水平只有行業的具體數量。

searchable do 
    integer :industry_ids[num_of_industries], multiple: true 

編輯: 我想通了

LIMITED_INDUSTRIES_BASED_ON_LEVEL = { 
    'Free' => 1, 
    'Silver' => 3, 
    'Gold' => 5, 
    'Platinum' => 5 
} 
def industries_limited 
    self.industries.limit(LIMITED_INDUSTRIES_BASED_ON_LEVEL[self.level]) 
end 
searchable do 
    integer :industry_ids, multiple: true do 
    self.industries_limited.map(&:id) 
    end 
end 
+0

您談論公司,水平,像我們知道你的要求的行業。請添加相關細節,因爲我們無法承擔。請提供更高層次的問題概述以及相關問題的模式。 – aitchnyu

+0

對不起,我不是很清楚,我明白了。 – gt565k

回答

0

想通了 公司的水平是說「自由」,因此被限制到1名的行業,但它可以增加更多,所以在未來如果它決定升級,所有的信息都在那裏。所以我限制搜索本身基本上只計算行業的基礎上的水平(在這種情況下只計算第1,因爲級別=「免費」,限制爲1)

LIMITED_INDUSTRIES_BASED_ON_LEVEL = { 
    'Free' => 1, 
    'Silver' => 3, 
    'Gold' => 5, 
    'Platinum' => 5 
} 
def industries_limited 
    self.industries.limit(LIMITED_INDUSTRIES_BASED_ON_LEVEL[self.level]) 
end 
searchable do 
    integer :industry_ids, multiple: true do 
    self.industries_limited.map(&:id) 
    end 
end