【问题标题】:Rails ActiveRecord includes where with likeRails ActiveRecord 包括 where with like
【发布时间】:2018-10-04 15:35:20
【问题描述】:

我有这个查询,我需要在这个查询中使用,帮助我。

范围 :filter, -> (query) { includes(:city).where(cities: {name: query}) }

【问题讨论】:

    标签: ruby-on-rails postgresql activerecord


    【解决方案1】:

    只需使用字符串作为where 的参数

    scope :filter, -> (query) { joins(:city).where('cities.name LIKE ?', "%#{ query }%") }
    

    【讨论】:

    • 新代码:scope :filter, -> (query) { includes(:city).where('cities.name LIKE ?', "%#{ query }%") } 当我使用此方法返回错误:ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: missing FROM-clause entry for table "cities" LINE 1: ...hoods" WHERE (neighborhoods.id NOT IN (170)) AND (cities.名字
    • @JhonnyPamponet,更新了我的答案,将 includes 替换为 joins
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-28
    • 2011-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多