我怎样才能遍历这个ruby数组?

时间:2022-06-02 08:50:58

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.

我正在尝试以下代码,但是在控制台中它只返回整个微博阵列。我想获取用户发布的每个微博的tag_list。

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

在我想要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?

如何获取所有帖子的tag_list?

Sorry for the noob question

抱歉,这个菜鸟问题

2 个解决方案

#1


1  

Try with:

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

#2


2  

Try this:

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

#1


1  

Try with:

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

#2


2  

Try this:

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