Ruby on rails Rails 3.1将对象添加到find_by_sql/can';不能在erb文件中显示

Ruby on rails Rails 3.1将对象添加到find_by_sql/can';不能在erb文件中显示,ruby-on-rails,ruby-on-rails-3,activerecord,erb,Ruby On Rails,Ruby On Rails 3,Activerecord,Erb,我正在将一个php应用程序移植到rails,所以我有一组sql语句,我正在转换这些语句,以便通过sql查找。我看到它返回了我调用它的类型的对象集合。我想做的是遍历这个集合(可能是一个数组),然后添加一个特定对象的实例,如下所示: #class is GlobalList #sql is simplified - really joining across 3 tables def self.common_items user_ids items=find_by_sql(["select

我正在将一个php应用程序移植到rails,所以我有一组sql语句,我正在转换这些语句,以便通过sql查找。我看到它返回了我调用它的类型的对象集合。我想做的是遍历这个集合(可能是一个数组),然后添加一个特定对象的实例,如下所示:

#class is GlobalList
#sql is simplified - really joining across 3 tables
def self.common_items user_ids
    items=find_by_sql(["select gl.global_id, count(gl.global_id) as global_count from main_table gl group by global_id"])

    #each of these items is a GlobalList and want to add a location to the Array            
    items.each_with_index do |value,index|
        tmp_item=Location.find_by_global_id(value['global_id'])
        #not sure if this is possible
        items[index]['location']=tmp_item
    end
    return items
end
控制器

#controller  
@common_items=GlobalList.common_items user_ids
看法

#查看代码-第三行不起作用
全球统一标识:
所以我有三个问题:
1.项目是数组吗?它说这是通过调用.class实现的,但不确定
2.我可以添加这些全球列表项目的位置。但是在视图代码中,我无法访问location的属性。我将如何访问此文件?

3.我知道这很难看-有没有更好的模式来实现这一点?

我会从sql查询中的位置获取您需要的任何数据,这样可以避免

然后在视图中:

<% @items.each do |gl| %>
        <%= gl.location_foo %> <-- select any fields you want in controller -->
        <%= gl.global_count %>
        <%= gl.global_id %>
<% end %>
然后在视图中:


<% @items.each do |gl| %>
        <%= gl.location_foo %> <-- select any fields you want in controller -->
        <%= gl.global_count %>
        <%= gl.global_id %>
<% end %>
<% @common_items.each do |value,location| %>
        <%=debug(location) %> 
        global_id:<%=value.global_id %> 
<% end %>

全球统一标识:

我同意,但我希望这些find\u by\u sql是一个临时解决方案。另外,我们将把这个问题推到memcache上,这样就不用担心n+1问题了(还有一个上限是10)。雇员再培训局文件中的“三重呼吁”是否破坏了交易?thxit不是交易破坏者,但我不明白为什么需要将位置写回返回的模型中。参见编辑

def self.common_items user_ids
    items=find_by_sql(["select gl.global_id, count(gl.global_id) as global_count 
                        from main_table gl group by global_id"])

    items.map do |value|
        [value, Location.find_by_global_id(value.global_id)]
    end
end
<% @common_items.each do |value,location| %>
        <%=debug(location) %> 
        global_id:<%=value.global_id %> 
<% end %>