开发者

Rails 3. Query for single items

开发者 https://www.devze.com 2023-03-16 16:14 出处:网络
I am developing an API using Rails 3 and I am implementing a tag search. Each user can add their own custom tags, therefore when a user searches for a tag it is returned several times. For example whe

I am developing an API using Rails 3 and I am implementing a tag search. Each user can add their own custom tags, therefore when a user searches for a tag it is returned several times. For example when someone is searching for drupalcon and 10 people has added it it will appear ten times in the search results. I want it to appear only on time per tag.

开发者_运维知识库This is my query code. How can I extend it to do such a search?

@tags = Tag.where("LOWER (tag) LIKE ?", "%#{@query}%").limit(1)

UPDATE

Solved it with this query.

@tags = Tag.select('DISTINCT tag').where("LOWER (tag) LIKE ?", "%#{@query}%")

Thankful for all input!


You call Tag.first and pass it a conditions option.

@tag = Tag.first(:conditions => ["LOWER (tag) LIKE ?", "%#{@query}%"])
0

精彩评论

暂无评论...
验证码 换一张
取 消