# Trying to do a report with a list of zip codes and getting stuck

**URL:** <https://www.ruby-forum.com/t/trying-to-do-a-report-with-a-list-of-zip-codes-and-getting-stuck/194766>\
**Category:** Rails\
**Created:** [September 10, 2010, 6:42am UTC](https://www.ruby-forum.com/t/trying-to-do-a-report-with-a-list-of-zip-codes-and-getting-stuck/194766 "2010-09-10T06:42:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [September 10, 2010, 6:42am UTC](https://www.ruby-forum.com/t/trying-to-do-a-report-with-a-list-of-zip-codes-and-getting-stuck/194766/1 "2010-09-10T06:42:27Z")

</div>

I’m trying to get a list of zip codes in my database with

zipstring = sprintf("select distinct zip from visits,households where  
" +  
"households.id=visits.household\_id and visits.month=‘%2d’ and  
visits.year = ‘%4d’ " +  
“and visits.monthly = 1 order by households.zip;”, datestart.month,  
datestart.year, dateend, datestart)

@ziplist = Household.find\_by\_sql(zipstring)

this gives me an array of zip codes. Then I use

\<%= render :partial=\>“monthly\_report”, :collection =\> @ziplist %\>

to run monthly\_report for each zip code. This is where the problem is.  
If I try to put the variable into a find call, it isn’t text.

If I look at it in the debugger, it’s

#\<Household zip: “01501”\>

and .to\_s is

“#Household:0xb68ad9e4”

All I need is the 01501 part, but I’m getting the memory address in my  
find call. Please help.

Thanks

Bob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [September 10, 2010, 6:44am UTC](https://www.ruby-forum.com/t/trying-to-do-a-report-with-a-list-of-zip-codes-and-getting-stuck/194766/2 "2010-09-10T06:44:48Z")

</div>

On Sep 10, 12:41 am, Bob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> and .to\_s is
> 
> “#Household:0xb68ad9e4”
> 
> All I need is the 01501 part, but I’m getting the memory address in my  
> find call. Please help.
> 
> Thanks
> 
> Bob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

By the way, those variables are monthly\_report from the  
render :partial call.

Bob

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [September 10, 2010, 9:39am UTC](https://www.ruby-forum.com/t/trying-to-do-a-report-with-a-list-of-zip-codes-and-getting-stuck/194766/3 "2010-09-10T09:39:48Z")

</div>

On 10 September 2010 05:41, Bob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> and .to\_s is
> 
> “#Household:0xb68ad9e4”
> 
> All I need is the 01501 part, but I’m getting the memory address in my  
> find call. Please help.

Can you not just use .zip on each element to get what you want?

Colin

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [September 10, 2010, 8:58pm UTC](https://www.ruby-forum.com/t/trying-to-do-a-report-with-a-list-of-zip-codes-and-getting-stuck/194766/4 "2010-09-10T20:58:20Z")

</div>

On Sep 10, 11:52 am, Philip H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > @ziplist = Household.find\_by\_sql(zipstring)  
> > > #\<Household zip: “01501”\>  
> > > He could. Or since he’s bypassing AR (for the most part) it might be easier to do…
> 
> @ziplist = Household.connection.select\_values(zipstring)
> 
> From the docs…  
> select\_values(sql, name = nil)  
> Returns an array of the values of the first column in a select:  
> select\_values(“SELECT id FROM companies LIMIT 3”) =\> [1,2,3]
> 
> -philip

I ended up with the .zip method. Didn’t realize that the response from  
the select was a collection, even with only one field.

Thanks  
Bob

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [September 10, 2010, 5:59pm UTC](https://www.ruby-forum.com/t/trying-to-do-a-report-with-a-list-of-zip-codes-and-getting-stuck/194766/5 "2010-09-10T17:59:47Z")

</div>

> > @ziplist = Household.find\_by\_sql(zipstring)  
> > #\<Household zip: “01501”\>
> > 
> > and .to\_s is
> > 
> > “#Household:0xb68ad9e4”
> > 
> > All I need is the 01501 part, but I’m getting the memory address in my  
> > find call. Please help.
> 
> Can you not just use .zip on each element to get what you want?

He could. Or since he’s bypassing AR (for the most part) it might be  
easier to do…

@ziplist = Household.connection.select\_values(zipstring)

From the docs…  
select\_values(sql, name = nil)  
Returns an array of the values of the first column in a select:  
select\_values(“SELECT id FROM companies LIMIT 3”) =\> [1,2,3]

-philip
