【问题标题】:Raw sql in railsrails中的原始sql
【发布时间】:2019-06-27 09:22:35
【问题描述】:
scope :not_null, -> (params) { where("data ILIKE 'string'", key: params['key']) }

我想要的是从 data 列或给定的字符串中检查

scope :not_null, -> { where("'random_string' ILIKE 'string'") }

我想要的范围必须支持我给出的列名或字符串,是否有可能拥有这样的范围

【问题讨论】:

  • 在这种情况下,最好定义一个类方法而不是使用范围!

标签: sql ruby-on-rails activerecord


【解决方案1】:

你可以使用 arel:

scope :scopename, ->(keyname=nil){
  where(arel_table[keyname || :data].matches('string'))
}

【讨论】:

  • 你能详细说明@Vasfed吗?
  • @Rthi arel_table[column_name] 用于引用该列,matches 是“ilike”的代表,因此此构造查询类似于where("passed_keyname_or_'data'_when_not ilike 'string'")
猜你喜欢
  • 2013-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-02
  • 1970-01-01
  • 2016-03-27
  • 2013-06-11
  • 2014-01-11
相关资源
最近更新 更多