Define models and associations and let your QB write your SQL.