DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
SQLListColumn
a string/text column with comma-separated values
class SQLListColumn < String
SEPARATOR = ','
def contains(value)
"#{self} like '%#{SEPARATOR}#{value}#{SEPARATOR}%' or #{self} like '#{value}#{SEPARATOR}%' or #{self} like '%#{SEPARATOR}#{value}' or #{self} = '#{value}'"
end
end
Usage:
SQLListColumn.new('admins').contains('tmorgan')
...gives you a string you can use in a SQL query:
"admins like '%,tmorgan,%' or admins like 'tmorgan,%' or admins like '%,tmorgan' or admins = 'tmorgan'"
Rails usage might look like this:
Program.find(
:all,
:conditions => \
SQLListColumn.new('admins').contains(username) \
+ ' or ' + \
SQLListColumn.new('viewers').contains(username),
:order => 'name'
)






Comments
Snippets Manager replied on Mon, 2012/05/07 - 2:36pm
Snippets Manager replied on Fri, 2006/06/09 - 10:24am