Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've got a series of Posts and would like to select all posts where its title size is lesser than 30, how to do that?

Posts.where("len(title) < 30")? 
like image 253
andersonleite Avatar asked Sep 29 '10 19:09

andersonleite


1 Answers

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.

like image 158
Raphomet Avatar answered Oct 15 '22 12:10

Raphomet