I have been using ActiveRecord's find_by_sql to obtain the number of
records within a date range from a mysql database like this:

result = Xyz.find_by_sql("SELECT COUNT(*) as recordcount FROM xyz where
rdate > '#{start_date}' and rdate < '#{end_date}'")[0].recordcount

The problem with the above approach is that it is tied to mysql. Is
there a way that I can accomplish the above in a database independent
way?

Thanks for any input.

... doug

--
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@googlegroups.com.
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-US.

Search Discussions

  • Dave Aronson at Jul 12, 2012 at 7:32 pm

    On Thu, Jul 12, 2012 at 3:18 PM, Doug Jolley wrote:

    result = Xyz.find_by_sql("SELECT COUNT(*) as recordcount FROM xyz where
    rdate > '#{start_date}' and rdate < '#{end_date}'")[0].recordcount

    The problem with the above approach is that it is tied to mysql. Is
    there a way that I can accomplish the above in a database independent
    way?
    Something like:

    Xyz.where("rdate BETWEEN ? and ?", start_date, end_date).count

    or:

    Xyz.where(rdate: start_date..end_date).count

    ought to work just fine. The latter is shorter, but I think I'd favor
    the clarity of the former (or maybe I'm just not sufficiently used to
    that syntax yet). Check out:

    http://guides.rubyonrails.org/active_record_querying.html

    for all sorts of useful ActiveRecord query methods.

    -Dave

    --
    Dave Aronson, Cleared/Remote Ruby on Rails Freelancer
    (NoVa/DC/Remote); see www.DaveAronson.com, and blogs at
    www.Codosaur.us, www.Dare2XL.com, www.RecruitingRants.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@googlegroups.com.
    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-US.
  • Robert Walker at Jul 12, 2012 at 10:02 pm

    Doug Jolley wrote in post #1068482:
    I have been using ActiveRecord's find_by_sql to obtain the number of
    records within a date range from a mysql database like this:

    result = Xyz.find_by_sql("SELECT COUNT(*) as recordcount FROM xyz where
    rdate > '#{start_date}' and rdate < '#{end_date}'")[0].recordcount

    The problem with the above approach is that it is tied to mysql. Is
    there a way that I can accomplish the above in a database independent
    way?
    That's not the only problem with this style of query. There's also
    potentially serious security problems with it as well.

    You should read the guide on securing Rails applications. Specifically
    for this case read up on SQL Injection:

    http://guides.rubyonrails.org/security.html#sql-injection

    --
    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@googlegroups.com.
    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-US.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedJul 12, '12 at 7:18p
activeJul 12, '12 at 10:02p
posts3
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Robert Walker: 2 posts Dave Aronson: 1 post

People

Translate

site design / logo © 2022 Grokbase