How do you filter active records by date? I''m trying to get only entries where the date is between two other dates, but the filtering in the code below does not actually filter anything: @from_date = Date.new(params[:from_date][:year].to_i, params[:from_date][:month].to_i, params[:from_date][:day].to_i) @to_date = Date.new(params[:to_date][:year].to_i, params[:to_date] [:month].to_i, params[:to_date][:day].to_i) @entries = @account.entries.find_all{|entry|entry.date > @from_date and entry.date < @to_date} @entries contains everything from @account.entries, and does not filter anything out, regardless of the dates. If I use the ruby script/console, and pull out a random entry from the database. I can compare its date with a created date on the console, yielding a correct true or false, but inside of find_all, I do not get the correct results. I''ve tried "hardcoding" the @from_date and @to_date like so: (@from_date = Date.new(2007,9,1), but it seems to have no effect. The date field on entries is of type "date" (it was created using a migration). I''m using SQLite as the database. Any help is appreciated! --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Sep 18, 2007, at 8:23 AM, Dave Roberts wrote:> How do you filter active records by date? > I''m trying to get only entries where the date is between two other > dates, but the filtering in the code below does not actually filter > anything: > > @from_date = Date.new(params[:from_date][:year].to_i, > params[:from_date][:month].to_i, params[:from_date][:day].to_i) > @to_date = Date.new(params[:to_date][:year].to_i, params[:to_date] > [:month].to_i, params[:to_date][:day].to_i) > @entries = @account.entries.find_all{|entry|entry.date > @from_date > and entry.date < @to_date}You''re using a deprecated form of the API here and syntactically giving a block (which will be ignored). Try this: @entries = @account.entries.find(:all, :conditions => [ "date BETWEEN ? and ?", @from_date.to_s(:db), @to_date.to_s(:db) ])> > @entries contains everything from @account.entries, and does not > filter anything out, regardless of the dates. If I use the ruby > script/console, and pull out a random entry from the database. I can > compare its date with a created date on the console, yielding a > correct true or false, but inside of find_all, I do not get the > correct results. > > I''ve tried "hardcoding" the @from_date and @to_date like so: > (@from_date = Date.new(2007,9,1), but it seems to have no effect. > The date field on entries is of type "date" (it was created using a > migration). I''m using SQLite as the database. > > Any help is appreciated! >I hope this helps! The SQL syntax for something like this should be the same as MySQL (which I use). -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org
I am filtering records by date with the find method and with the newer API mentioned above, but I do not convert the date to a string with the ''to_s'' method. I believe that Active Record with the ''find'' method does this for you. If, on the other hand, you were to use ''find_by_sql'', you would need to format the date as a string with single quotes and in a format that your particular database can recognize and evaluate correctly. For that reason, the ''find_by_sql'' method seems less portable to another database whose date string formats may not completely overlap with anothers'' formats. On Sep 18, 8:41 am, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote:> On Sep 18, 2007, at 8:23 AM, Dave Roberts wrote: > > > How do you filter active records by date? > > I''m trying to get only entries where the date is between two other > > dates, but the filtering in the code below does not actually filter > > anything: > > > @from_date = Date.new(params[:from_date][:year].to_i, > > params[:from_date][:month].to_i, params[:from_date][:day].to_i) > > @to_date = Date.new(params[:to_date][:year].to_i, params[:to_date] > > [:month].to_i, params[:to_date][:day].to_i) > > @entries = @account.entries.find_all{|entry|entry.date > @from_date > > and entry.date < @to_date} > > You''re using a deprecated form of the API here and syntactically > giving a block (which will be ignored). Try this: > > @entries = @account.entries.find(:all, > :conditions => [ "date BETWEEN ? and ?", > @from_date.to_s(:db), > @to_date.to_s(:db) ]) > > > > > @entries contains everything from @account.entries, and does not > > filter anything out, regardless of the dates. If I use the ruby > > script/console, and pull out a random entry from the database. I can > > compare its date with a created date on the console, yielding a > > correct true or false, but inside of find_all, I do not get the > > correct results. > > > I''ve tried "hardcoding" the @from_date and @to_date like so: > > (@from_date = Date.new(2007,9,1), but it seems to have no effect. > > The date field on entries is of type "date" (it was created using a > > migration). I''m using SQLite as the database. > > > Any help is appreciated! > > I hope this helps! The SQL syntax for something like this should be > the same as MySQL (which I use). > > -Rob > > Rob Biedenharn http://agileconsultingllc.com > R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org > > smime.p7s > 3KDownload--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Arthur Lyman wrote:> I am filtering records by date with the find method and with the newer > API mentioned above, but I do not convert the date to a string with > the ''to_s'' method. I believe that Active Record with the ''find'' > method does this for you. If, on the other hand, you were to use > ''find_by_sql'', you would need to format the date as a string with > single quotes and in a format that your particular database can > recognize and evaluate correctly. For that reason, the ''find_by_sql'' > method seems less portable to another database whose date string > formats may not completely overlap with anothers'' formats. > > On Sep 18, 8:41 am, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org>yeah. this comes through "sanitize_sql_array", which uses "connection.quote", which calls "quote_date" "if value.acts_like?(:date) || value.acts_like?(:time)" where "quote_date" just calls "to_s(:db)" on it. -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---