Luca G. Soave
Luca G. Soave

Reputation: 12679

Access to ruby matrix

I've got a data structure (matrix) like the following :

ruby-1.9.2-p290 :060 >   current_user.watchlists.map{|l|l.tags_array}
 => [[], [], ["tag1", "tag2", "tag3"], [], [], ["tag2", "tag3"], [], [], ["tag4"], []] 
ruby-1.9.2-p290 :061 >

I want to traverse the structure to get a result like this:

"tag1" "tag2" "tag3" "tag4"

How can I do it ?

Upvotes: 1

Views: 72

Answers (1)

clyfe
clyfe

Reputation: 23770

current_user.watchlists.map(&:tags_array).flatten.uniq.sort

Even better, if you can, make User has_many :tags, :trough => :watchlists and then:

current_user.tags.order_by(:name)

Upvotes: 3

Related Questions