Return all rows, split then show uniques


#1

I’m returning some rows from a tags database that look like this:

ID WORDS

  1. apple banana pear
  2. banana melon
  3. apple peach lime

What I want to do with that data is use the .split method to divide them
into separate values in an array, then use the .uniq method to return a
unique list of the words like so:

apple, banana, pear, melon, peach, lime

So in my controller I have:

def taglist
@tags = Tag.chop_em(params[:tag])
end

and in my model I know I need something:

def self.chopem(tag)
Tag.find(:all)
end

I think I lack the understanding of how the array that comes back from
the database is constructed. Can anyone explain this so I can chop those
words up and sort them with the .uniq method?

Apologies if this is arse for elbow.


#2

Paul Livingstone wrote:

I’m returning some rows from a tags database that look like this:

ID WORDS

  1. apple banana pear
  2. banana melon
  3. apple peach lime

What I want to do with that data is use the .split method to divide them
into separate values in an array, then use the .uniq method to return a
unique list of the words like so:

apple, banana, pear, melon, peach, lime

So in my controller I have:

def taglist
@tags = Tag.chop_em(params[:tag])
end

and in my model I know I need something:

def self.chopem(tag)
Tag.find(:all)
end

I think I lack the understanding of how the array that comes back from
the database is constructed. Can anyone explain this so I can chop those
words up and sort them with the .uniq method?

Apologies if this is arse for elbow.

hi,

you can examine how it is constructed with the inspect method. I guess
what is returned here is an array of objects with a “words” attribute,
so you probably need to get at this attribute of each element in this
array, concat them all into one string and then do the split and uniq.


Agnieszka


#3

I suggest you better use acts_as_taggable as your current database
schema
looks very bad

http://dema.ruby.com.br/articles/2005/09/03/tagging-on-steroids-with-rails