简体   繁体   English

如何使用Rails查询获取区分大小写的数据

[英]How to get case-sensitive data with rails query

Assuming there is a community who has both "apple" and "APPLE" tags. 假设有一个社区同时具有“ apple”和“ APPLE”标签。

Then if I execute this with the param "apple" 然后,如果我使用参数“ apple”执行此操作

@communities = Community.joins(taggings: :tag).where(tags: { name: params[:tag] })

In result page, 2 sets of the same community come appears. 在结果页面中,出现2个相同社区的集合。
It's probably because it's fetching with the params[:tag] for both upper case and lower case. 可能是因为它使用大写和小写的params [:tag]获取。

How can I strict this and make it not like search? 我该如何严格执行,使其like搜索like I want case-sensitive and complete match. 我要区分大小写和完全匹配。

尝试将属性名称包含在二进制文件中。

 @communities = Community.joins(taggings: :tag).where(['binary(tags.name) = ?', params[:tag]])

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM