Is there a way to select records by the size of a string field using ARel? For example if I have a bunch of users, can I select all user's whose first names are 3 or fewer characters? I can do something like the following with strings
User.where('LENGTH("users"."firstname") < 3')
I'd like to be able to do something like
User.where(User.arel_table[:firstname].length.lt(3)
Unfortunately no. That being said, the reason you try to use Arel functionality is to avoid database dependent SQL functionality (e.g. Postgres and Mysql have different date handling functions). LENGTH()
, on the other hand, is a standard SQL function, and you won't have problems with portability of that query.
tl;dr - Using custom SQL for everything is bad practice. Using custom SQL strategically is totally fine.