2014-04-15 23 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

相关问题