Solutions to SQL antipatterns in Rails