# Date\_select with Searchlogic

**URL:** <https://www.ruby-forum.com/t/date-select-with-searchlogic/184117>\
**Category:** Rails\
**Created:** [February 25, 2010, 12:27pm UTC](https://www.ruby-forum.com/t/date-select-with-searchlogic/184117 "2010-02-25T12:27:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [February 25, 2010, 12:27pm UTC](https://www.ruby-forum.com/t/date-select-with-searchlogic/184117/1 "2010-02-25T12:27:10Z")

</div>

I am aware that date selects params are supposed to supported by  
Searchlogic.

But I’m battling to get them working.

I am using one of the named\_scopes that it makes, and it works perfectly  
in the console:

User.created\_at\_gte(1.year.ago)

this behaves as expected.

In my view I have this:

- form\_for @search do |f|  
Surname  
=f.text\_field :surname\_like  
Age between  
=f.text\_field :age\_gte  
=f.text\_field :age\_lte  
Joined after  
=f.date\_select :created\_at\_gte

the error I get is when actually using the field, I get this error:  
The is not a valid condition. You may only use conditions that map to a  
named scope

and the params are:

{“commit”=\>“Search”,  
“search”=\>{“created\_at\_gte(1i)”=\>“2011”,  
“created\_at\_gte(2i)”=\>“2”,  
“created\_at\_gte(3i)”=\>“25”,  
“age\_gte”=\>“5”,  
“order”=\>"",  
“surname\_like”=\>"",  
“age\_lte”=\>“25”}}

Now unless I’m wrong, searchlogic should be able to parse the params  
with their date groups. Or it’s just being stupid and thinking  
“created\_at\_gte(1i)” is a named scope. In which case I’m going to have  
to alter the params to make it friendly for searchlogic.

anythink I’m missing?

---

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [February 27, 2010, 11:44am UTC](https://www.ruby-forum.com/t/date-select-with-searchlogic/184117/2 "2010-02-27T11:44:43Z")

</div>

+1, hit this nail right on today ☹

---

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [March 2, 2010, 11:53am UTC](https://www.ruby-forum.com/t/date-select-with-searchlogic/184117/3 "2010-03-02T11:53:04Z")

</div>

William Y. wrote:

> +1, hit this nail right on today ☹

Sorry for the late reply, u probably moved on from this.

I got a method that takes all the date params from the params and  
changes them to a friendlier version.

def parse\_date\_params(param\_hash)  
hash = param\_hash.clone  
regexp = /(.+)((\d)[a-zA-Z])/  
dates\_arrays = hash.select{|key, value| key.to\_s.match(regexp) }

```
hash.delete_if{|key, value| key.to_s.match(regexp) }

add_to_hash = {}

dates_arrays.each do |a|
  matchdata = a[0].match(regexp)
  # check for all three.
  add_to_hash[matchdata[1]] ? 

```

add\_to\_hash[matchdata[1]][matchdata[2]] = a[1] :  
add\_to\_hash[matchdata[1]] = {matchdata[2] =\> a[1]}  
end

```
add_to_hash.each_pair { |key, value| hash[key] = 

```

Date.civil(value[“1”].to\_i, value[“2”].to\_i,value[“3”].to\_i)}

```
return hash

```

end

let me know if that helps.
