简体   繁体   English

在 Rails 查询中包含 LIKE 子句的最佳方法是什么?

[英]What's the best way to include a LIKE clause in a Rails query?

What's the best way to include a LIKE clause in a Rails query ie something along the lines of (the completely incorrect):在 Rails 查询中包含 LIKE 子句的最佳方式是什么,即(完全不正确):

 Question.where(:content => 'LIKE %farming%')

You'd use the syntax:您将使用以下语法:

Question.where("content LIKE ?" , "%#{farming}%")

If this is Rails 3 you can use Arel's matches .如果这是 Rails 3,您可以使用 Arel 的matches This has the advantage of being database agnostic.这具有与数据库无关的优点。 For example:例如:

Question.where(Question.arel_table[:content].matches("%#{string}%"))

This is somewhat clunky, but easily extracted to scopes, eg:这有点笨重,但很容易提取到范围,例如:

class Question

  def self.match_scope_condition(col, query)
    arel_table[col].matches("%#{query}%")
  end

  scope :matching, lambda {|*args|
    col, opts = args.shift, args.extract_options!
    op = opts[:operator] || :or
    where args.flatten.map {|query| match_scope_condition(col, query) }.inject(&op)
  }

  scope :matching_content, lambda {|*query|
    matching(:content, *query)
  }
end

Question.matching_content('farming', 'dancing') # farming or dancing
Question.matching_content('farming', 'dancing', :operator => :and) # farming and dancing
Question.matching(:other_column, 'farming', 'dancing') # same thing for a different col

Of course to join with "AND" you could just chain the scopes.当然要加入“AND”,您可以链接范围。

Edit: +1 to metawhere and squeel though (haven't tried latter but it looks cool) They both add this type of functionality and much more.编辑:虽然 +1 到 metawhere 和 squeel(没有尝试过后者,但看起来很酷)他们都添加了这种类型的功能等等。

If you want really sexy conditions and and don't have problems with external dependencies, I highly recommend MetaWhere and it's successor Squeel :如果你想要真正性感的条件并且没有外部依赖的问题,我强烈推荐MetaWhere ,它是Squeel的继任者:

# MetaWhere
Question.where(:content.like => '%farming%')

# MetaWhere with operators overloaded
Question.where(:content =~ '%farming%')

# Squeel
Question.where { :content.matches => '%farming%' }

# Squeel with operators overloaded
Question.where { :content =~ '%farming%' }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在HABTM连接表的Rails查询中包含LIKE子句 - How to include a LIKE clause in Rails query on HABTM join table 什么是测试查询Rails性能的最佳方法 - What is the best way to test performance of the query Rails 在Rails中安排和执行重复任务(比如抓取信息页面)的最佳方法是什么? - What's the best way to schedule and execute repetitive tasks (like scraping a page for information) in Rails? 在 Rails 4 中实现角色的最佳方式是什么? - What's the best way to implement roles in Rails 4? 在Rails中按日期搜索的最佳方法是什么? - What's the best way to search by date in Rails? 在Rails中生成书签的最佳方法是什么? - What's the best way to generate bookmarklet in Rails? 重构此Rails控制器的最佳方法是什么? - What's the best way to refactor this Rails controller? 使用ActiveRecord使用OR子句进行查询的“路线”是什么? - What is the “Rails Way” of doing a query with an OR clause using ActiveRecord? 在Rails中在哪里编写“(x AND y)OR(a AND b)”的最佳方法是什么? - What's the best way to write a “(x AND y)OR(a AND b)” where query in Rails? Rails 3在范围内编写IN子句的最佳方法是什么? - Rails 3 best way to write an IN clause in a scope?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM