date_select in a select query?

hi, stuck with using the date_select tag in a search screen.

So in my search form i've got this...

<%=date_select(:from, '', :start_year => 1950, :include_blank => true)%>

which gives back this in the param...

"select * from users where created_at >= '#{params[:from]}'"

and this in the sql string

"select * from users where created_at >= '(3i)4(1i)1953(2i)5'"

any ideas how to get params[:from] to return something a bit more like a date, e.g. '1953/4/5'

really stuck?

John Griffiths wrote:

any ideas how to get params[:from] to return something a bit more like a date, e.g. '1953/4/5'

params[:from].to_s(:db)

hth ilan

Look at your development.log (or the console window if you've got your server running there). You can see that the parameter from is actually a hash {'(1i)' => '1953', '(2i)' => '5', '(3i)' => 4}, ie. the date components are passed separately. If instead of using date_select you use select_date, you'll even get friendly names in there ({'year' => 1953, 'month' => 5, 'day' => 3}). All you need to do is put those back together into a date. Somehow this feels like the most frequently asked question this week, so the other threads about date_select & select_date might also help you.

Fred

Totally agree Fred, I had a hunt around in the forums and there were masses of posts on the subject but no clear resolution.

I thought it best to post as much info as I could to try to resolve this, glad about the response.

I tried using select_date but couldn't get it working how i wanted. I did find a post suggesting passing the param into a date object, thanks Ilan for your solution.

Will try this,

found this, looks along the lines...

http://blog.ericgoodwin.com/tags/snippets

ugh... everytime i tried using that i got a 'wrong number of arguments' error

wrote a function to split the hash up and put it back together as a good date...

  def convert_date(obj)     @a = ''     @b = ''     @c = ''

    for @d in params[:reg_from]       if @d.to_s.index('(1i)') : @a = @d.to_s[4,4] end       if @d.to_s.index('(2i)') : @b = @d.to_s[4,2] end       if @d.to_s.index('(3i)') : @c = @d.to_s[4,2] end     end

    return "#{@a}-#{@b}-#{@c}"   end

...not the best way but gets results, any advances on this solution?

sorrry....

  def convert_date(obj)     @a = ''     @b = ''     @c = ''

    for @d in obj       if @d.to_s.index('(1i)') : @a = @d.to_s[4,4] end       if @d.to_s.index('(2i)') : @b = @d.to_s[4,2] end       if @d.to_s.index('(3i)') : @c = @d.to_s[4,2] end     end

    return "#{@a}-#{@b}-#{@c}"   end

sorrry....

There's easier: Time.mktime(params[:somedate]['(1i)'], params[:somedate]['(2i)'], params[:somedate]['(3i)']) (or use Date::civil instead of Time.mktime if you want an instance of date rather than time).

Fred

Thanks Fred, I tried hunting around for some extra info and examples for converting a string to a date or time but couldn't find it (nothing in the agile & programming books), appreciate this solution.

I'll test it out and see what happens,

all the best,

John.

Thanks, solved it in the end with your help, really appreciated.

def convert_date(obj)   return Date.new(obj['(1i)'].to_i,obj['(2i)'].to_i,obj['(3i)'].to_i) end

had to put the .to_i to convert each to an integer

posted on my blog for posterity, glad this is over,

big thanks Fred,

http://www.red91.com/articles/2008/03/19/date_select-conversion

Frederick, If this is such a commonly asked question then why doesn't someone write an easy to follow example of taking a date from user input on a non(form_for) view and then load it into the model.date_field. I swear I've looked at countless examples on the internet and no one takes it home. Could someone be a prince and make this happen? Kathleen

Frederick, If this is such a commonly asked question then why doesn't someone write an easy to follow example of taking a date from user input on a non(form_for) view and then load it into the model.date_field. I swear I've looked at countless examples on the internet and no one takes it home.

Looks like John did just that :slight_smile: I must admit I'm a bit surprised: it
shouldn't be that hard to figure out, especially compared to a lo of
the conceptually much harder things in ruby and/or rails.

Fred

Thanks Frederick and Kathy, appreciate that, :wink:

So happy we cracked that one, really had me stumped, thanks again for helping out.

Take care and have a great Easter hols, you've earned it!

John.