Forum: Ruby find_by_sql and date_format

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Ea3d530684e6ac7110d158d33c592f64?d=identicon&s=25 felix sheng (Guest)
on 2007-03-26 04:19
(Received via mailing list)

I'm beginning my first project in Ruby (and Rails) and running into
an issue. I need to pull some data out of mysql using the date_format
command but ActiveRecord is not appreciating my sql. I'm doing
something like:

  Table.find_by_sql( [ 'select column1, date_format("%Y%m%d", column2)
as column2 from table where column1 = ?', some_data ] );

More or less it looks like that. The key problem seems to be the
date_format has %'s in it and it gives me:

  malformed format string - %Y

And the trace:

active_record/base.rb:1418:in `%'
active_record/base.rb:1418:in `sanitize_sql_array'
active_record/base.rb:1387:in `sanitize_sql'
active_record/base.rb:427:in `find_by_sql'

Is there any way to handle this situation? I also tried using the raw
connection, but that didn't seem to take bind variables which I found
unsatisfying, so didn't pursue it too closely.

Any help/pointers are greatly appreciated.

05e48e632fdd0b2c25d27042f52c11d5?d=identicon&s=25 Alex LeDonne (Guest)
on 2007-03-26 19:51
(Received via mailing list)
On 3/25/07, felix sheng <> wrote:
> More or less it looks like that. The key problem seems to be the
>         /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
> felix


According to
, the function should be called as:


That is, the format is the SECOND argument.

This topic is locked and can not be replied to.