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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en-US.
On Thu, Jul 12, 2012 at 3:18 PM, Doug Jolley <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en-US.
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en-US.