meta_search icon indicating copy to clipboard operation
meta_search copied to clipboard

Can I use a check_box to implement a date related search? (i.e. show only expired accounts)

Open theSociableme opened this issue 13 years ago • 2 comments

This works fine, however I want to use it as a check box

 <%= f.text_field :user_profile_expiration_lt, :value => Date.today.to_s(:long) %>

if checked then result set contains only where results with expiration less then today.

i.e. Show only expired []

Any ideas how to do this?
or implement a named scope based off of check_box ?

theSociableme avatar Jan 02 '12 19:01 theSociableme

Use a scope.

class Model < ActiveRecord::Base
  scope :expired, lambda { where(['user_profile_expiration_lt < ?', Date.today]) }
  search_methods :expired
end
<%= f.check_box :expired %>

graywh avatar Mar 02 '12 21:03 graywh

I've just been hit by a bug using this method @graywh, if I declare my scope with a lambda, I get the following error: wrong number of arguments (1 for 0) for scope :with_qty, lambda { where(:id.lt => 100) } If I remove the lambda, it works: scope :with_qty, where(:id.lt => 100)

To make it work, I need to painfully add a management of the checkbox value, this way: scope :with_qty, lambda { |cb| cb == '1' ? where(:id.lt => 100) : scoped} which makes me change all my scopes to manage the checkbox version... very ugly.

gamov avatar Dec 21 '12 07:12 gamov