[LRUG] Conditionally built SQL suitable for Active Record/Rails?

gvim gvimrc at gmail.com
Sun Feb 28 13:12:06 PST 2016

I'm converting a Perl web application to Rails which contains a lot of 
conditionally-built SQL such as this:

sub log_row_sql {
   my $master = shift;
   my $sql  = q(SELECT 
masterid,firstname,surname,sex,age,address,towncity,postcode,l.county AS 
      $sql .= q(email,dancer,updates,source,rooms, 
      $sql .= q(ch2surname,ch2sex,ch2age,to_char(eventdate,'DD Mon 
YYYY') as eventdate,eventmusic,eventinfo,participants,);
      $sql .= q(vouchernum,pin,to_char(voucherexp,'DD Mon YYYY') as 
voucherexp,expcode,a.header as subject, a.title,);
      $sql .= q(a.type, a.template, p.header as dancestyle, p.text as 
package, l.packageid, p.package as abbrev,);		
      $sql .= 
      $sql .= q(,location,district,postarea,m.county AS 
provcounty,pemail,pemail2,pname,ptel,pmobile) unless $master eq 'e';
      $sql .= q( FROM log_form l, activities a, packages p);
      $sql .= q(, vmaster m) unless $master eq 'e';
      $sql .= q( WHERE l.id = ?);
      $sql .= q( AND l.packageid = p.id);
      $sql .= q( AND l.activityid = a.id);
      $sql .= q( AND l.masterid = m.id) unless $master eq 'e';
   return $sql;

It seems this is not a good fit for Active Record and maybe Rails in 


More information about the Chat mailing list