ActiveRecord select a string field has a certain length in Rails 3?

This should work:

Post.where("length(title) < 30")

You’re correctly using #where as shorthand for :conditions in Rails 3. You can pass in any snippet that works in your local SQL directly.

Just remember that ActiveRecord model classes are singular by convention.

Leave a Comment