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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 18, 12:31 am, John Griffiths <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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... >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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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, -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
found this, looks along the lines... http://blog.ericgoodwin.com/tags/snippets -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 18 Mar 2008, at 22:05, John Griffiths wrote:> > 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> > > > 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 > > -- > Posted via http://www.ruby-forum.com/. > > --~--~---------~--~----~------------~-------~--~----~ > You received this message because you are subscribed to the Google > Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en > -~----------~----~----~----~------~----~------~--~--- >
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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
KathysKode-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Mar-20 01:28 UTC
Re: date_select in a select query?
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 On Mar 18, 1:57 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Mar 18, 12:31 am, John Griffiths <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote:> 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... > > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 20 Mar 2008, at 01:28, KathysKode-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> > 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 :-) 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> > Could someone be a prince and make this happen? > Kathleen > > On Mar 18, 1:57 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> On Mar 18, 12:31 am, John Griffiths <rails-mailing-l...@andreas- >> s.net> >> wrote:> 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... >> >> 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 > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Frederick and Kathy, appreciate that, ;-) 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---