Bob Smith
2010-Sep-08 19:04 UTC
Trying to local variable passed to a partial in a find call and getting nowhere..
My code is.. controller 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) view - monthly.html.erb <%= render :partial=>"monthly_report", :collection => @ziplist %> view - _monthly_report.html.erb <%= @oxtotalstring = Household.find(:all, :conditions => ["households.zip = ?,households.id=visits.household_id, visits.month=?, visits.year = ?, visits.monthly = ''1''", monthly_report, datestart.month, datestart.year ], :include => :visits, :order => "zip" ) %> model class Visit < ActiveRecord::Base belongs_to :household class Household < ActiveRecord::Base has_many :visits, :dependent => :destroy when I try this, the monthly_report variable in the partial looks like this.. Household_zip: "01537" The only reason I''m using a find call here is that later in the code I need to render a partial for each record found, then see child tables from that variable, as in monthly_report.people. From what I''ve seen, this functionality is lost using find_by_sql. if I remove the ? and monthly report from the find call and put a sample zip code in, it works fine. Is there some way to translate this to a string I''m missing? I tried monthly_report.to_s and got the same result. Thanks for the help 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.
radhames brito
2010-Sep-08 20:35 UTC
Re: Trying to local variable passed to a partial in a find call and getting nowhere..
first of all, that huge chunk of code in you controller should go in your model, controllers are suppose to remain thin and models are suppose to be fat, second use named scopes that query you have could be very small and readable with named scopes, and use helpers you have a mess of code in you view and those are the main problems. On Wed, Sep 8, 2010 at 3:04 PM, Bob Smith <bsm2th-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> My code is.. > controller > 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) > > view - monthly.html.erb > <%= render :partial=>"monthly_report", :collection => @ziplist %> > > view - _monthly_report.html.erb > <%= @oxtotalstring = Household.find(:all, :conditions => > ["households.zip = ?,households.id=visits.household_id, > visits.month=?, visits.year = ?, visits.monthly = ''1''", > monthly_report, datestart.month, datestart.year ], :include > => :visits, :order => "zip" ) %> > > model > class Visit < ActiveRecord::Base > belongs_to :household > class Household < ActiveRecord::Base > has_many :visits, :dependent => :destroy > > > > when I try this, the monthly_report variable in the partial looks like > this.. > Household_zip: "01537" > > The only reason I''m using a find call here is that later in the code I > need to render a partial for each record found, then see child tables > from that variable, as in monthly_report.people. From what I''ve seen, > this functionality is lost using find_by_sql. > > if I remove the ? and monthly report from the find call and put a > sample zip code in, it works fine. Is there some way to translate this > to a string I''m missing? I tried monthly_report.to_s and got the same > result. > > > Thanks for the help > > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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.
Dharmdip Rathod
2010-Sep-09 07:02 UTC
Re: Trying to local variable passed to a partial in a find call and getting nowhere..
Use monthly_report["Household_zip"] -- Posted via http://www.ruby-forum.com/. -- 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.