debugging

Find the bug, Rails edition

Blog

I have this code in a model in a Rails project.

  validates :name, presence: true, on: :create
  validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }, on: create

Let's put it in an Author model:

class Author 

When retrieving a list of all of the authors without email addresses (because, well, we are cleaning up these things) with this line of code:

  authors = Author.where(:email: nil)

... two new blank authors are created at the WHERE query. Both of their names and emails are blank. This is absurd!

Find the bug.

Show MySQL warnings after query

Snippet

When a query has run, a number of warnings may have occurred that didn't stop the query from running, but may be of concern. Use "SHOW WARNINGS [LIMIT n]" to display the warnings and see what might have gone wrong in the query.

mysql> SELECT name, addr1, addr2, city, state, FROM_UNIXTIME(created_on) AS created FROM `orders` WHERE ...;
Empty set, 20180 warnings (0.02 sec)
 
mysql> show warnings limit 10;
+---------+------+---------------------------------------------------------+
| Level   | Code | Message                                                 |
+---------+------+---------------------------------------------------------+
| Warning | 1292 | Truncated incorrect DOUBLE value: '2009-08-21 00:01:01' | 
| Warning | 1292 | Truncated incorrect DOUBLE value: '2009-08-26 00:01:01' | 
| Warning | 1292 | Truncated incorrect DOUBLE value: '2009-08-21 00:01:01' | 
| Warning | 1292 | Truncated incorrect DOUBLE value: '2009-08-26 00:01:01' | 

Print evaluated query string using ADODB/PHP

Snippet

After a parameterized querystring has been evaluated, but before the query has been run, print out the query string, using ADODB PHP library.

<?php
 
/*
 * include the ADODB library
 */
include('adodb.inc.php');
 
/*
 * Make a new connection to DB
 */
$mydb = &ADONewConnection('access');     
$mydb->PConnect('northwind');  
 
/*
 * filter input
 */
$shipto = $conn->qstr("John's Old Shoppe");
 
/*
 * generate SQL
 */
$sql = "insert into orders (customerID,EmployeeID,OrderDate,ShipName) ";
$sql .= "values ('ANATR',2,".$mydb->FormatDate(time()).",?)";
 
/*
 *
 *
 * PRINT THE SQL BEFORE EXECUTING THE QUERY
 *
 *
 */
$mydb->debug = true;
 
if ($mydb->Execute($sql, $shipto) === false) print 'error inserting';
 
?>