APJ
APJ

Reputation: 245

How can I loop through this ruby array?

I'm trying the following code however in the console it just returns the entire microposts array. I want to get the tag_list for each micropost that a user has posted.

As in I want User.find(1).microposts[0..-1].tag_list

User.find(1).microposts.each {|micropost| micropost.tag_list}

How can I get the tag_list for all posts?

Sorry for the noob question

Upvotes: 0

Views: 51

Answers (2)

carpamon
carpamon

Reputation: 6623

Try with:

User.find(1).microposts.map(&:tag_list).flatten

Upvotes: 1

Mike Li
Mike Li

Reputation: 3406

Try this:

User.find(1).microposts.map(&:tag_list)

Upvotes: 2

Related Questions