Rails将.each值的集合转换为Array-type

时间:2022-06-15 16:24:36

I am using a .each method to find a list of genres

我正在使用.each方法查找类型列表

<% @mg_relationships.each do |mg_relationship| %>
    <% @genre = Genre.find(mg_relationship.genre_id) %>
       <%= @genre.id %>
<% end %>

This code returns the following

此代码返回以下内容

35 14 10595 10751 35 28 12 14 878 35

What i need to be able to do is to, outside of the each call, have the above output transformed into an array-like format, called @array for example, as such:

我需要做的是,在每个调用之外,将上面的输出转换为类似于数组的格式,例如@array,如下:

[35, 14, 10595, 10751, 35, 28, 12, 14, 878, 35]

So that i can then use <%= @array.uniq! %> to filter how to similar values and return

这样我就可以使用<%= @ array.uniq了! %>过滤类似值的方法并返回

[35, 14, 10595, 10751, 28, 12, 878]

4 个解决方案

#1


1  

You don't need to query the Genre model, you already have genre_id. Just use pluck, on the model or association:

您不需要查询类型模型,您已经有genre_id。只需在模型或关联上使用pluck:

MgRelationship.pluck(:genre_id)

If you need the results to be unique, you can apply .uniq to the scope to generate a select distinct:

如果您需要结果是唯一的,则可以将.uniq应用于范围以生成select distinct:

MgRelationship.uniq.pluck(:genre_id)

#2


0  

Try this:

尝试这个:

<%= Genre.where(id: @mg_relationships.pluck(&:genre_id)).map(&:id) %>

#3


0  

A few of the answers above will do, but for what you want since you already have the @mg_relationships variable how about just simply:

上面的一些答案可以做到,但是因为你已经拥有@mg_relationships变量,你想要的只是简单的:

@mg_relationships.map{|mg| mg.genre_id}.uniq!

@ mg_relationships.map {|毫克| mg.genre_id} .uniq!

#4


0  

Meagar's answer is quite well, but I want to offer another way.

Meagar的答案非常好,但我想提供另一种方式。

Genre.where('exists (select 1 from Mg_Relationships 
             where Mg_Relationships.genre_id = genres.id)').pluck(:id)

It have the same effect of MgRelationship.uniq.pluck(:genre_id), but when the MgRelationships data includes some genres.id do not exists in genres, you can get rid of them at the same time.

它与MgRelationship.uniq.pluck(:genre_id)具有相同的效果,但是当MgRelationships数据包含某些类型时。在类型中不存在,你可以同时摆脱它们。

EDIT:

编辑:

I reviewed your question, realized that we may all missunderstand you.

我回顾了你的问题,意识到我们可能都会误解你。

You said you want outside of the each call, to get @arrary. I guess you just want fetch DBMS once, to populate two kinds of content to the view.

你说你想在每次通话之外得到@arrary。我想你只想要一次获取DBMS,将​​两种内容填充到视图中。

If I guess so, try this:

如果我这么认为,试试这个:

<% genre_ids = []%>
<% @mg_relationships.each do |mg_relationship| %>
    <% @genre = Genre.find(mg_relationship.genre_id) %>
       <%#= @genre.id %>
       <% genre_ids << @genre.id  %>
<% end %>

<% = genre_ids %>
<% = genre_ids.uniq %>

In veiws, You can write any ruby code without output to browser just with <% %> instead of <% = ...%>

在veiws中,您可以使用<%%>而不是<%= ...%>编写任何没有输出到浏览器的ruby代码

But we have to say your original code have big problem about efficiency, that's why all the answers are suggest you to change to map or pluck.

但我们不得不说你的原始代码在效率方面存在很大问题,这就是为什么所有答案都建议你改为映射或拔除。

If you have 10 mg_relationships, your original code will generate 11 sql statements to execute. All the answers to suggest you is to generate only one sql statement to do the same thing.

如果你有10 mg_relationships,你的原始代码将生成11个sql语句来执行。建议你的所有答案是只生成一个sql语句来做同样的事情。

Wish you can understand our suggestion, and improve your code.

希望您能理解我们的建议,并改进您的代码。

#1


1  

You don't need to query the Genre model, you already have genre_id. Just use pluck, on the model or association:

您不需要查询类型模型,您已经有genre_id。只需在模型或关联上使用pluck:

MgRelationship.pluck(:genre_id)

If you need the results to be unique, you can apply .uniq to the scope to generate a select distinct:

如果您需要结果是唯一的,则可以将.uniq应用于范围以生成select distinct:

MgRelationship.uniq.pluck(:genre_id)

#2


0  

Try this:

尝试这个:

<%= Genre.where(id: @mg_relationships.pluck(&:genre_id)).map(&:id) %>

#3


0  

A few of the answers above will do, but for what you want since you already have the @mg_relationships variable how about just simply:

上面的一些答案可以做到,但是因为你已经拥有@mg_relationships变量,你想要的只是简单的:

@mg_relationships.map{|mg| mg.genre_id}.uniq!

@ mg_relationships.map {|毫克| mg.genre_id} .uniq!

#4


0  

Meagar's answer is quite well, but I want to offer another way.

Meagar的答案非常好,但我想提供另一种方式。

Genre.where('exists (select 1 from Mg_Relationships 
             where Mg_Relationships.genre_id = genres.id)').pluck(:id)

It have the same effect of MgRelationship.uniq.pluck(:genre_id), but when the MgRelationships data includes some genres.id do not exists in genres, you can get rid of them at the same time.

它与MgRelationship.uniq.pluck(:genre_id)具有相同的效果,但是当MgRelationships数据包含某些类型时。在类型中不存在,你可以同时摆脱它们。

EDIT:

编辑:

I reviewed your question, realized that we may all missunderstand you.

我回顾了你的问题,意识到我们可能都会误解你。

You said you want outside of the each call, to get @arrary. I guess you just want fetch DBMS once, to populate two kinds of content to the view.

你说你想在每次通话之外得到@arrary。我想你只想要一次获取DBMS,将​​两种内容填充到视图中。

If I guess so, try this:

如果我这么认为,试试这个:

<% genre_ids = []%>
<% @mg_relationships.each do |mg_relationship| %>
    <% @genre = Genre.find(mg_relationship.genre_id) %>
       <%#= @genre.id %>
       <% genre_ids << @genre.id  %>
<% end %>

<% = genre_ids %>
<% = genre_ids.uniq %>

In veiws, You can write any ruby code without output to browser just with <% %> instead of <% = ...%>

在veiws中,您可以使用<%%>而不是<%= ...%>编写任何没有输出到浏览器的ruby代码

But we have to say your original code have big problem about efficiency, that's why all the answers are suggest you to change to map or pluck.

但我们不得不说你的原始代码在效率方面存在很大问题,这就是为什么所有答案都建议你改为映射或拔除。

If you have 10 mg_relationships, your original code will generate 11 sql statements to execute. All the answers to suggest you is to generate only one sql statement to do the same thing.

如果你有10 mg_relationships,你的原始代码将生成11个sql语句来执行。建议你的所有答案是只生成一个sql语句来做同样的事情。

Wish you can understand our suggestion, and improve your code.

希望您能理解我们的建议,并改进您的代码。