[LRUG] Active Record find_by_sql head-scratcher

gvim gvimrc at gmail.com
Fri Jul 15 06:03:30 PDT 2016

In Rails console this Active Record query:

logrow = LogForm.find_by_sql([log_row_sql(5339), 18998])[0]

... produces the output:

LogForm Load (17.0ms)  SELECT 
AS usercounty,tel,mobile,email,dancer,updates,source,rooms, 
Mon YYYY') as 
Mon YYYY') as voucherexp,expcode,a.header as subject, a.title,a.type, 
a.template, p.header as dancestyle, p.text as package, l.packageid, 
p.package as 
AS provcounty,pemail,pemail2,pname,ptel,pmobile FROM log_form l, 
activities a, packages p, vmaster m WHERE l.id = 18998 AND l.packageid = 
p.id AND l.activityid = a.id AND l.masterid = m.id

.... followed by:

#<LogForm:0x007faeb396b1d0 id: nil, activityid: 9, packageid: 244, 
masterid: 5339, firstname: "Alan", surname: "Short", sex: "male", age: 
"adult", address: "12 Kember Road", towncity: "London", postcode: 
"SW81FG", tel: "02077629045", mobile: nil, email: 
"alan.short276 at gmail.com", dancer: nil, updates: "yes", source: nil, 
eventdate: nil, eventmusic: nil, eventinfo: nil, vouchernum: "asd123", 
expcode: nil, voucherexp: Thu, 08 Aug 2019, rooms: nil, occupancy: nil, 
ch1firstname: nil, ch1surname: nil, ch1sex: nil, ch1age: nil, 
ch2firstname: nil, ch2surname: nil, ch2sex: nil, ch2age: nil, 
participants: nil, person2_firstname: nil, person2_surname: nil, 
person2_sex: nil, level: nil, pin: nil>

Many fields are missing in the second part of the output but in Rails 
console but I can access these missing attributes, eg. logrow.template 
... or ... logrow[:template] and both return the data: "standard". 
However, the same AR query in my application returns nil for 
logrow[:template] despite the Rails error page showing the same value 
for logrow as the second part of the console output (above).

It appears these missing fields are only available within Rails console, 
for some reason, as the same values (5339, 18998) are passed to the same 
query in the app.


More information about the Chat mailing list