Forum: Ruby on Rails datetime select

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2f1b77757fbee66ef826844b647a8b4c?d=identicon&s=25 Katsuo Isono (katsuo)
on 2008-10-06 13:16
I am having difficulty passing datetime values to a method in the
controller. I get sql syntax error from the following. Can someone
advise me what I am doing wrong?

my view:

    <table>
      <tr>
        <%= start_form_tag :action => 'time_range' %>
        <td ><%=  datetime_select("sd", "time_range") %></td>
        <td ><%=  datetime_select("ed", "time_range") %></td>
        <td><%= submit_tag 'go'%></td>
      </tr>
    </table>

my method:

def time_range
  start_date=params[:sd]
  end_date=params[:ed]
  @orders=Order.find(:all,:conditions => ['created_at BETWEEN ? AND ?',
start_date, end_date])

end
Ab9027f6c7941589420e84ec6488fcd1?d=identicon&s=25 Shripad (Guest)
on 2008-10-06 13:41
(Received via mailing list)
hi,

you can do this,

 @start_date = Date.civil(params[:range]
[:"start_date(1i)"].to_i,params[:range]
[:"start_date(2i)"].to_i,params[:range][:"start_date(3i)"].to_i)
            @end_date = Date.civil(params[:range]
[:"end_date(1i)"].to_i,params[:range]
[:"end_date(2i)"].to_i,params[:range][:"end_date(3i)"].to_i)

thanks and regards,
shripad

On Oct 6, 4:16 pm, Katsuo Isono <rails-mailing-l...@andreas-s.net>
2f1b77757fbee66ef826844b647a8b4c?d=identicon&s=25 Katsuo Isono (katsuo)
on 2008-10-07 11:25
Thanks Shripad
From your advice I've got the following. It worked fine until I added
hours and minutes to the range which are attributes 4i and 5i. I don't
get any error messages but don't get values from db either. Datetime
format in the db is 2003-10-14 12:37:51. Any suggestions would be
appreciated.

my view:

    <table>
      <tr>
        <%= start_form_tag :action => 'time_range' %>
        <td ><%=  datetime_select("sd", "range") %></td>
        <td ><%=  datetime_select("ed", "range") %></td>
        <td><%= submit_tag 'go'%></td>
      </tr>
    </table>

my method:

def time_range
  @start_date = DateTime.civil(params[:sd][:"range(1i)"].to_i,
params[:sd][:"range(2i)"].to_i,
params[:sd][:"range(3i)"].to_i,params[:sd][:"range(4i)"].to_i,
params[:sd][:"range(5i)"].to_i)

  @end_date =
DateTime.civil(params[:ed][:"range(1i)"].to_i,params[:ed][:"range(2i)"].to_i,params[:ed][:"range(3i)"].to_i,params[:ed][:"range(4i)"].to_i,
params[:ed][:"range(5i)"].to_i)

  @orders=Order.find(:all,:conditions => ['created_at BETWEEN ? AND ?',
@start_date, @end_date])

end
Ab9027f6c7941589420e84ec6488fcd1?d=identicon&s=25 Shripad (Guest)
on 2008-10-07 13:24
(Received via mailing list)
Hi,
First tell me which database u r using?
And, Please execute the last query against database, check whether it
is working fine or not

thanks and regards,
Shripad

On Oct 7, 2:25 pm, Katsuo Isono <rails-mailing-l...@andreas-s.net>
2f1b77757fbee66ef826844b647a8b4c?d=identicon&s=25 Katsuo Isono (katsuo)
on 2008-10-09 09:35
Thanks for reply Shripad;

Db is Mysql. Following is the sql running for the last query:
 'select paydate,payamount,credit,paymenttype,memberid from payments
where paydate between '2008-10-09T14:22:00+00:00' AND
'2008-10-10T14:22:00+00:00'

Format in db is 2003-10-14 12:37:51

Katsuo
519d971b4225781ff523082fff137f31?d=identicon&s=25 mahmoud said (Guest)
on 2008-10-09 10:37
(Received via mailing list)
Greetings Everyone..

In the conditions of your query you should pass the parameters as simple
strings.
User strftime to get the desired string representation while passing it
to
the query
I'd try this:

@orders=Order.find(:all,:conditions => ['created_at BETWEEN "?" AND
"?"',
start_date.strftime('%Y-%m-%d %H:%M:%S'), end_date.strftime('%Y-%m-%d
%H:%M:%S')])

the format above is the one u mentioned as the default of your database
engine "Format in db is 2003-10-14 12:37:51"




On Mon, Oct 6, 2008 at 1:16 PM, Katsuo Isono <
rails-mailing-list@andreas-s.net> wrote:

>        <td ><%=  datetime_select("sd", "time_range") %></td>
>  @orders=Order.find(:all,:conditions => ['created_at BETWEEN ? AND ?',
> start_date, end_date])
>
> end
> --
> Posted via http://www.ruby-forum.com/.
>
> >
>


--
Mahmoud Said
Software Developer
blog.modsaid.com
www.eSpace.com.eg
+20-16-1223857
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-10-09 10:47
(Received via mailing list)
On 9 Oct 2008, at 09:18, mahmoud said wrote:

> start_date.strftime('%Y-%m-%d %H:%M:%S'), end_date.strftime('%Y-%m-
> %d %H:%M:%S')])
>
> the format above is the one u mentioned as the default of your
> database engine "Format in db is 2003-10-14 12:37:51"
>

Looks like you're using rails 1.1 or earlier. In more recent versions
of rails you don't need to do any of this:

Order.find(:all,:conditions => ['created_at BETWEEN ?
AND ?',start_date, end_date])
would just work

start_date.to_s(:db) might work, but I really can't remember what
works on really old versions of rails.

Fred
519d971b4225781ff523082fff137f31?d=identicon&s=25 mahmoud said (Guest)
on 2008-10-09 13:25
(Received via mailing list)
Thanks for the tip Fred.

I hadn't tried Order.find(:all,:conditions => ['created_at BETWEEN ?
AND ?',start_date, end_date]) myself... it does work on rails 2.1.0

Mahmoud

On Thu, Oct 9, 2008 at 10:46 AM, Frederick Cheung <
frederick.cheung@gmail.com> wrote:

> > I'd try this:
> Looks like you're using rails 1.1 or earlier. In more recent versions
>
> > controller. I get sql syntax error from the following. Can someone
> >     </tr>
> > end
> > blog.modsaid.com
> > www.eSpace.com.eg
> > +20-16-1223857
> >
> >
> > >
>
>
> >
>


--
Mahmoud Said
Software Developer
blog.modsaid.com
www.eSpace.com.eg
+20-16-1223857
2f1b77757fbee66ef826844b647a8b4c?d=identicon&s=25 Katsuo Isono (katsuo)
on 2008-10-10 09:45
I've setteled for the following and works great!

@orders=Order.find(:all,:conditions => ['created_at BETWEEN "?" AND
"?"',
start_date.strftime('%Y-%m-%d %H:%M:%S'), end_date.strftime('%Y-%m-%d
%H:%M:%S')])

I am using 1.8 but somehow just start_date and end_date without
formatting did not work.

BIG THANKS TO YOU ALL
Katsuo
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-10-10 09:55
(Received via mailing list)
On Oct 10, 8:45 am, Katsuo Isono <rails-mailing-l...@andreas-s.net>
wrote:

> I am using 1.8 but somehow just start_date and end_date without
> formatting did not work.

That's ruby 1.8. Rails version are independant of that

Fred
This topic is locked and can not be replied to.