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. -- Posted via http://www.ruby-forum.com/.
Agnieszka Figiel
2006-Mar-24 17:06 UTC
[Rails] Re: Return all rows, split then show uniques
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 -- Posted via http://www.ruby-forum.com/.
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 On 3/24/06, Agnieszka Figiel <agnieszka.figiel@gmail.com> wrote:> > 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060324/cae16013/attachment.html
Reasonably Related Threads
- [LLVMdev] Request to Bitter Melon regarding the LLVM demo page
- [LLVMdev] Request to Bitter Melon regarding the LLVM demo page
- pattern instead of colors in bargraph
- Stories with many tags, tags with many stories, has_and_belongs_to_many howto?
- [LLVMdev] simulating c style unions in LLVM