开发者

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

开发者 https://www.devze.com 2023-01-17 23:00 出处:网络
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?

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")?


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.


This works for me with MySQL: Post.find(:all, :conditions => "length(title) < 30")

0

精彩评论

暂无评论...
验证码 换一张
取 消