Foo
Foo

Reputation: 810

How to get case-sensitive data with rails query

Assuming there is a community who has both "apple" and "APPLE" tags.

Then if I execute this with the param "apple"

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

In result page, 2 sets of the same community come appears.
It's probably because it's fetching with the params[:tag] for both upper case and lower case.

How can I strict this and make it not like search? I want case-sensitive and complete match.

Upvotes: 0

Views: 357

Answers (1)

Rahul Tapali
Rahul Tapali

Reputation: 10137

Try enclosing attribute name in binary.

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

Upvotes: 4

Related Questions