Bob Smith
2010-Sep-10 04:41 UTC
Trying to do a report with a list of zip codes and getting stuck..
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 Smith <bsm2th-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Bob Smith
2010-Sep-10 04:43 UTC
Re: Trying to do a report with a list of zip codes and getting stuck..
On Sep 10, 12:41 am, Bob Smith <bsm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 Smith <bsm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>By the way, those variables are monthly_report from the render :partial call. Bob -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2010-Sep-10 07:36 UTC
Re: Trying to do a report with a list of zip codes and getting stuck..
On 10 September 2010 05:41, Bob Smith <bsm2th-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.Can you not just use .zip on each element to get what you want? Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Philip Hallstrom
2010-Sep-10 15:52 UTC
Re: Trying to do a report with a list of zip codes and getting stuck..
> On 10 September 2010 05:41, Bob Smith <bsm2th-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> 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. > > 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Bob Smith
2010-Sep-10 18:57 UTC
Re: Trying to do a report with a list of zip codes and getting stuck..
On Sep 10, 11:52 am, Philip Hallstrom <phi...-LSG90OXdqQE@public.gmane.org> wrote:> > On 10 September 2010 05:41, Bob Smith <bsm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> 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. > > > 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] > > -philipI 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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.