Hi guys, I''m having a hard time with a scope within an ActiveRecord model. Here''s my class: https://gist.github.com/1622314 The scope is named "current" and should select records between two dates and here is what happens: https://gist.github.com/1622334 This is the output for a rails console session, it contains logging to the STDOUT so you''ll see the executed SQL commands there. Here''s a quick explanation for that console output: Line 1 shows there''s no Message record. Line 4 creates a Message Line 14 calls the "current" named scope that should fetch the recently created message and shows how this message is not retrieved. However, if I exit the console and then re-enter the console, Message.current does retrieve the recently created record. I''m using Rails 3.1.3 and Ruby 1.9.3-p0. It wasn''t happening on Rails 3.0 and Ruby 1.9.2-p290 Any clues? Thanks a lot in advance. -- Leonardo Mateo. There''s no place like ~ -- 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.
This is not really a bug, but more due to the way Ruby evaluates when things are executed. scope :current, where("start <= ? AND expiration > ?", DateTime.now, DateTime.now) The code does use the current date/time, but the catch is that it doesn''t use the current date/time when the scope is called, but when the model is loaded. Meaning, when the console is loaded or the app is started. It never updates the date/time used for the scope once it''s loaded. You''ll want to use a Proc or lambda in this scenario, so the scope always uses the current date/time when the scope is called. scope :current, Proc.new { where("start <= ? AND expiration > ?", DateTime.now, DateTime.now) } That way, DateTime.now is only evaluated when the scope is called. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/jonkZutTBNoJ. 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.
On Mon, Jan 16, 2012 at 4:26 PM, Tim Shaffer <timshaffer-BUHhN+a2lJ4@public.gmane.org> wrote:> This is not really a bug, but more due to the way Ruby evaluates when things > are executed. > > scope :current, where("start <= ? AND expiration > ?", DateTime.now, > DateTime.now) > > The code does use the current date/time, but the catch is that it doesn''t > use the current date/time when the scope is called, but when the model is > loaded. Meaning, when the console is loaded or the app is started. It never > updates the date/time used for the scope once it''s loaded. > > You''ll want to use a Proc or lambda in this scenario, so the scope always > uses the current date/time when the scope is called. > > scope :current, Proc.new { where("start <= ? AND expiration > ?", > DateTime.now, DateTime.now) } > > That way, DateTime.now is only evaluated when the scope is called.Hi Tim, Great point. That was, in fact, the problem. Thanks a lot. -- Leonardo Mateo. There''s no place like ~ -- 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.