# Retrieving records using like condition

**URL:** <https://www.ruby-forum.com/t/retrieving-records-using-like-condition/186443>\
**Category:** Rails\
**Created:** [April 7, 2010, 9:57am UTC](https://www.ruby-forum.com/t/retrieving-records-using-like-condition/186443 "2010-04-07T09:57:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bungoman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/77aa72/32.png) [@bungoman](https://www.ruby-forum.com/u/bungoman)\
**Post date:** [April 7, 2010, 9:57am UTC](https://www.ruby-forum.com/t/retrieving-records-using-like-condition/186443/1 "2010-04-07T09:57:30Z")

</div>

So I’ve got a model called Item and one of its properties is called  
“tags”. It’s just string that is a simple comma separated list of tags  
associated with the item. I’m trying to do two things. First create a  
list of items with the most common tags (the top ten or so) and second  
allow the user to click on a link that will show all the items with the  
selected tag.

My problem is actually with the second task. Retrieving records that  
contain the tag “dog” for example is pretty simple, however I ran into a  
problem when I needed to retrieve records with the tag “not a dog”.  
Whenever I search for records with the tag “dog” it also returns the  
records with the tag “not a dog” because “dog” is in “not a dog”.

It took me a while but I got something that does what I want it to do,  
but I’m just not convinced it’s the best way to go about doing things.  
Here’s what I got:

@items = Item.find(:all, :conditions =\> [‘tags LIKE ? and folder\_id =  
?’, ‘%’ + params[:tag] + ‘%’, @folder.id])

@items.delete\_if { |item|  
tags = item.tags.split(’,’)

delete = true

for tag in tags  
tag.downcase!  
tag.strip!

```
if tag == params[:tag]
  delete = false

end

```

end

delete  
}

Whenever params[:tag] is “dog” this succeeds in only picking out the  
items that contain the tag “dog” but not ones that contain similar tags  
such as “not a dog”. But like I said it just seems icky and not  
cumbersome.

Is there some way to make the find command, or the SQL syntax for like  
handle searching through comma separated lists?

---

<div class="post-metadata">

**Author:** ![bungoman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/77aa72/32.png) [@bungoman](https://www.ruby-forum.com/u/bungoman)\
**Post date:** [April 7, 2010, 10:07am UTC](https://www.ruby-forum.com/t/retrieving-records-using-like-condition/186443/2 "2010-04-07T10:07:04Z")

</div>

On 7 April 2010 08:57, Sean E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So I’ve got a model called Item and one of its properties is called  
> “tags”. It’s just string that is a simple comma separated list of tags  
> associated with the item. I’m trying to do two things. First create a  
> list of items with the most common tags (the top ten or so) and second  
> allow the user to click on a link that will show all the items with the  
> selected tag.

Stop and refactor that into a “has\_many :tags”/“belongs\_to :item”  
association.  
Your life will be much easier.
