2014-04-15 25 views
1

我有一個名爲Vendor的型號。我有三個不同的模型與它相關聯。根據單列中出現的單詞數排序在導軌中

  1. 見證
  2. 服務
  3. 作品

我不得不查找每個表格,並列出供應商(極限5),其具有的字中的「實例」最多次這些模型之一中的一列。我如何在rails中執行此操作?


我有這樣的事情

Vendor.joins(:testimonials).where("testimonials.content LIKE ?","%example%") 

我需要找到誰的字「例子」

+0

這個列是字符串類型嗎? – RSB

+0

是的,cosider所有的列字符串與'文本'數據類型 – Aravind

+0

檢查我的答案 – RSB

回答

0

這就是我最終這樣做的結果。不知道我的問題是否被正確提問。我通過@ user3383458

vendor = Hash.new 
     Vendor.all.each do |v| 
      testi_word_count = 0 
      service_word_count = 0 
      title_word_count = 0 
      v.testimonials.each do |testi| 
       testi_word_count += testi.content.scan(/#{word_to_search}/).count 
       Rails.logger.info "testi_word_count" 
       Rails.logger.info testi_word_count 
      end 
      v.services.each do |service| 
       service_word_count += service.name.scan(/#{word_to_search}/).count 
       Rails.logger.info "service_word_count" 
       Rails.logger.info service_word_count 
      end 
      v.works.each do |work| 
       title_word_count += work.title.scan(/#{word_to_search}/).count 
       Rails.logger.info "title_word_count" 
       Rails.logger.info title_word_count 
      end 
      vendor[v]=testi_word_count + service_word_count + title_word_count 
     end 
0

在Ruby中出現的最大數量的供應商,你可以指望的發生例如

s = "this is a string with an example, example, example" 
s.scan(/example/).count # => 3 
+0

我有這樣的'Vendor.joins(:testimonials).where(「testimonials.content LIKE?」,「%example%」)''。我需要找到「example」這個詞出現次數最多的供應商。 – Aravind

+1

通過你的反對意見,你的意思是說你只是想使用sql,而不是任何後搜索的ruby代碼? –

1

我希望我能得到你的回報現在:

a=[] 
vendors.each do |v| 
    c=0 
    c=v.testimonial.scan(/example/).count 
    if c>0 
    a=a.concat([{vendor: v, counts: c}]) 
end 
+0

我認爲這將輸出計數,而我需要根據每個供應商的證詞「示例」的數量來訂購供應商。 – Aravind

+0

啊,好的。我會編輯它。稍等片刻。 – user3383458

+0

您是否需要每個供應商,包含單詞'example' ?? – user3383458

相關問題