Anders_P
2011-May-08 12:58 UTC
(Beginner) Finding a database object within a time interval
Hello, I''m trying to create a find statement that should find an element which start_date and end_date are within Time.now. If I for example have an object with a start_date of 2011-05-04 and end_date of 2011-05-19, the find statement should find the elment(s) that are within that time peroid. My code looks like this: def current_tee @current_time = Time.now @current_tee = Tee.find(:conditions => [ "id = ? AND featured => true AND start_date > #{@current_time} AND end_date <= #{@current_time}", params[:id] ]) end And: def show @tee = current_tee respond_to do |format| format.html # show.html.erb format.xml { render :xml => @tee } end end I keep getting this error: Couldn''t find Tee without an ID. Any tips on how to solve this? Thanks! // Anders -- 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.
Colin Law
2011-May-08 13:07 UTC
Re: (Beginner) Finding a database object within a time interval
On 8 May 2011 13:58, Anders_P <anders-TlBSdMGKk75YFVMCzM9+FrNAH6kLmebB@public.gmane.org> wrote:> Hello, > > I''m trying to create a find statement that should find an element > which start_date and end_date are within Time.now. If I for example > have an object with a start_date of 2011-05-04 and end_date of > 2011-05-19, the find statement should find the elment(s) that are > within that time peroid. My code looks like this: > > def current_tee > @current_time = Time.now > @current_tee = Tee.find(:conditions => [ > "id = ? AND featured => true AND start_date > #{@current_time} > AND end_date <= #{@current_time}", params[:id]Did you mean to require id = params[:id]? That means it can only ever find the one with that id, and then only if its time fields fit. Also note that this method would be better as a scope method of the Tee model, though that is not the cause of the error.> ]) > end > > And: > > def show > @tee = current_tee > respond_to do |format| > format.html # show.html.erb > format.xml { render :xml => @tee } > end > end > > I keep getting this error: Couldn''t find Tee without an ID.Have you checked that params[:id] actually contains a value? Have a look in development.log to see what params are being passed in. Also have a look at the Rails Guide on debugging and find out how to use ruby-debug to break into your code and examine data and follow flow. It is an invaluable technique when you cannot work out what is going on. Colin -- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-May-08 15:13 UTC
Re: (Beginner) Finding a database object within a time interval
On 8 May 2011, at 13:58, Anders_P <anders-TlBSdMGKk75YFVMCzM9+FrNAH6kLmebB@public.gmane.org> wrote:> Hello, > > I''m trying to create a find statement that should find an element > which start_date and end_date are within Time.now. If I for example > have an object with a start_date of 2011-05-04 and end_date of > 2011-05-19, the find statement should find the elment(s) that are > within that time peroid. My code looks like this: > > def current_tee > @current_time = Time.now > @current_tee = Tee.find(:conditions => [ > "id = ? AND featured => true AND start_date > #{@current_time} > AND end_date <= #{@current_time}", params[:id] > ]) > endYou either need Tee.find :all, :conditions => ... Or Tee.find some_id (or Tee.find :first, :conditions => ... if you only want one record) But you''ve got something halfway between the two Fred> > And: > > def show > @tee = current_tee > respond_to do |format| > format.html # show.html.erb > format.xml { render :xml => @tee } > end > end > > I keep getting this error: Couldn''t find Tee without an ID. > > Any tips on how to solve this? > > Thanks! > // Anders > > -- > 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. >-- 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.
Anders_P
2011-May-08 17:51 UTC
Re: (Beginner) Finding a database object within a time interval
Thanks for your answer, I think I got it to work. def current_tee current_time = Time.now @current_tee = Tee.find :all, :conditions => [ "featured = true AND start_date < ''#{current_time}'' AND end_date>= ''#{current_time}''"] end I had also forgot '' '' before and after ''#{current_time}''. Thanks! // Anders On May 8, 5:13 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 8 May 2011, at 13:58, Anders_P <and...-TlBSdMGKk75YFVMCzM9+FrNAH6kLmebB@public.gmane.org> wrote: > > > Hello, > > > I''m trying to create a find statement that should find an element > > which start_date and end_date are within Time.now. If I for example > > have an object with a start_date of 2011-05-04 and end_date of > > 2011-05-19, the find statement should find the elment(s) that are > > within that time peroid. My code looks like this: > > > def current_tee > > @current_time = Time.now > > @current_tee = Tee.find(:conditions => [ > > "id = ? AND featured => true AND start_date > #{@current_time} > > AND end_date <= #{@current_time}", params[:id] > > ]) > > end > > You either need Tee.find :all, :conditions => ... > > Or Tee.find some_id (or Tee.find :first, :conditions => ... if you only want one record) > > But you''ve got something halfway between the two > > Fred > > > > > > > And: > > > def show > > @tee = current_tee > > respond_to do |format| > > format.html # show.html.erb > > format.xml { render :xml => @tee } > > end > > end > > > I keep getting this error: Couldn''t find Tee without an ID. > > > Any tips on how to solve this? > > > Thanks! > > // Anders > > > -- > > 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 athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2011-May-08 18:02 UTC
Re: Re: (Beginner) Finding a database object within a time interval
On 8 May 2011, at 18:51, Anders_P <anders-TlBSdMGKk75YFVMCzM9+FrNAH6kLmebB@public.gmane.org> wrote:> Thanks for your answer, I think I got it to work. > > def current_tee > current_time = Time.now > @current_tee = Tee.find :all, :conditions => [ > "featured = true AND start_date < ''#{current_time}'' AND end_date >> = ''#{current_time}''" > ] > end > > I had also forgot '' '' before and after ''#{current_time}''. >If you use the placeholder syntax ( ie :conditions => ["start_date < ?", current_time]) then you don''t need to worry about that and other SQL escaping problems Fred> Thanks! > > // Anders > > > > On May 8, 5:13 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: >> On 8 May 2011, at 13:58, Anders_P <and...-TlBSdMGKk75YFVMCzM9+FrNAH6kLmebB@public.gmane.org> wrote: >> >>> Hello, >> >>> I''m trying to create a find statement that should find an element >>> which start_date and end_date are within Time.now. If I for example >>> have an object with a start_date of 2011-05-04 and end_date of >>> 2011-05-19, the find statement should find the elment(s) that are >>> within that time peroid. My code looks like this: >> >>> def current_tee >>> @current_time = Time.now >>> @current_tee = Tee.find(:conditions => [ >>> "id = ? AND featured => true AND start_date > #{@current_time} >>> AND end_date <= #{@current_time}", params[:id] >>> ]) >>> end >> >> You either need Tee.find :all, :conditions => ... >> >> Or Tee.find some_id (or Tee.find :first, :conditions => ... if you only want one record) >> >> But you''ve got something halfway between the two >> >> Fred >> >> >> >> >> >>> And: >> >>> def show >>> @tee = current_tee >>> respond_to do |format| >>> format.html # show.html.erb >>> format.xml { render :xml => @tee } >>> end >>> end >> >>> I keep getting this error: Couldn''t find Tee without an ID. >> >>> Any tips on how to solve this? >> >>> Thanks! >>> // Anders >> >>> -- >>> 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 athttp://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > 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. >-- 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.
Anders_P
2011-May-08 19:31 UTC
Re: (Beginner) Finding a database object within a time interval
Thanks for the tip, got a bit cleaner. On May 8, 8:02 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 8 May 2011, at 18:51, Anders_P <and...-TlBSdMGKk75YFVMCzM9+FrNAH6kLmebB@public.gmane.org> wrote: > > > Thanks for your answer, I think I got it to work. > > > def current_tee > > current_time = Time.now > > @current_tee = Tee.find :all, :conditions => [ > > "featured = true AND start_date < ''#{current_time}'' AND end_date > >> = ''#{current_time}''" > > ] > > end > > > I had also forgot '' '' before and after ''#{current_time}''. > > If you use the placeholder syntax ( ie :conditions => ["start_date < ?", current_time]) then you don''t need to worry about that and other SQL escaping problems > > Fred > > > > > Thanks! > > > // Anders > > > On May 8, 5:13 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > >> On 8 May 2011, at 13:58, Anders_P <and...-TlBSdMGKk75YFVMCzM9+FrNAH6kLmebB@public.gmane.org> wrote: > > >>> Hello, > > >>> I''m trying to create a find statement that should find an element > >>> which start_date and end_date are within Time.now. If I for example > >>> have an object with a start_date of 2011-05-04 and end_date of > >>> 2011-05-19, the find statement should find the elment(s) that are > >>> within that time peroid. My code looks like this: > > >>> def current_tee > >>> @current_time = Time.now > >>> @current_tee = Tee.find(:conditions => [ > >>> "id = ? AND featured => true AND start_date > #{@current_time} > >>> AND end_date <= #{@current_time}", params[:id] > >>> ]) > >>> end > > >> You either need Tee.find :all, :conditions => ... > > >> Or Tee.find some_id (or Tee.find :first, :conditions => ... if you only want one record) > > >> But you''ve got something halfway between the two > > >> Fred > > >>> And: > > >>> def show > >>> @tee = current_tee > >>> respond_to do |format| > >>> format.html # show.html.erb > >>> format.xml { render :xml => @tee } > >>> end > >>> end > > >>> I keep getting this error: Couldn''t find Tee without an ID. > > >>> Any tips on how to solve this? > > >>> Thanks! > >>> // Anders > > >>> -- > >>> 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-/JYPxA39Uh4Ykp1iOSErHA@public.gmane.orgm. > >>> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > >>> For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en. > > > -- > > 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 athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.