Forum: Ruby on Rails Return all rows, split then show uniques

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Paul Livingstone (Guest)
on 2006-03-24 16:44
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.
Agnieszka F. (Guest)
on 2006-03-24 19:06
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
Emin H. (Guest)
on 2006-03-24 21:56
(Received via mailing list)
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/taggin...
This topic is locked and can not be replied to.