[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

find_by_sql and date_format

felix sheng

3/26/2007 2:19:00 AM

Hello,

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:

/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/base.rb:1418:in `%'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/base.rb:1418:in `sanitize_sql_array'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
active_record/base.rb:1387:in `sanitize_sql'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
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.

felix

--
felix sheng
http://comments.d...




1 Answer

Alex LeDonne

3/26/2007 5:50:00 PM

0

On 3/25/07, felix sheng <felix@deasil.com> wrote:
> Hello,
>
> 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:
>
> /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
> active_record/base.rb:1418:in `%'
> /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
> active_record/base.rb:1418:in `sanitize_sql_array'
> /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
> active_record/base.rb:1387:in `sanitize_sql'
> /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/
> 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.
>
> felix
>

Felix,

According to http://dev.mysql.com/doc/refman/4.1/en/date-and-time-func...
, the function should be called as:

DATE_FORMAT(date,format)

That is, the format is the SECOND argument.

-A