Rails 3. Query for single items
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}%"])
精彩评论