Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6621017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:11:01+00:00 2026-05-25T21:11:01+00:00

I’m porting a php app to rails so I have a set of sql

  • 0

I’m porting a php app to rails so I have a set of sql statements that I’m converting to find_by_sql’s. I see that it returns a collection of objects of the type that I called on it. What I’d like to do is then iterate through this collection (presumably an array) and add an instance of a specific object like this:

#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

#controller  
@common_items=GlobalList.common_items user_ids

view

#view code - the third line doesn't work
<% @common_items.each_with_index do |value,key| %>
        <%=debug(value.location) %> <!-- works -->
        global_id:<%=value.location.global_id %> <!-- ### doesn't work but this is an attribute of this object-->
<% end %>

So I have 3 questions:
1. is items an Array? It says it is via a call to .class but not sure
2. I am able to add location these GlobalList items. However in the view code, I cannot access the attributes of location. How would I access this?
3. I know that this is pretty ugly – is there a better pattern to implement this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:11:02+00:00Added an answer on May 25, 2026 at 9:11 pm

    I would get any data you need from locations in the sql query, that way you avoid the n+1 problem

       @items=find_by_sql(["select locations.foo       as location_foo, 
                                   gl.global_id        as global_id, 
                                   count(gl.global_id) as global_count 
                            from main_table gl 
                            inner join locations on locations.global_id = gl.global_id 
                            group by global_id"])
    

    Then in the view:

    
    <% @items.each do |gl| %>
            <%= gl.location_foo %> <-- select any fields you want in controller -->
            <%= gl.global_count %>
            <%= gl.global_id %>
    <% end %>
    

    If you want to keep it close to as is, I would:

    
    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
    

    Then in the view:

    <% @common_items.each do |value,location| %>
            <%=debug(location) %> 
            global_id:<%=value.global_id %> 
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to create an if statement in PHP that prevents a single post
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.