= ? AND years <= ? \",min_age, 开发者_如何学Cmax_age])" />
开发者

find condition issue

min_age = params[:min_age]

max_age = params[:max_age]

@users = User.find(:all, :conditions => [" years >= ? AND years <= ? ", min_age, 开发者_如何学Cmax_age])

This is the controller method for a search feature. As you can see I wish to search by age range. This works for all max_age values up to 99, but when the max_age exceeds 99 the search returns no results when it should. I don't really expect many people beyond 99 but I am curious why this is happening. The age is a string.


It's because "100" comes before "20" when you compare alphanumerically as strings do. Change age to an integer as it should be, and it will work fine.


Mark Byers has the right answer. So it should be accepted.

But you should know, this is the kind of thing you should be using a named scope for.

In user.rb

named_scope :ages_in_range, lambda {|min, max|
  {:conditions => ["years >= ? AND years <= ?", min, max]}
}

Now in your controller:

@users = User.ages_in_range(min_age,max_age)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜