简体   繁体   中英

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

尝试这个:

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

尝试:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM