if model.compiled_this_week.first.empty?
OR
if model.compiled_this_week.find(:first).empty?
.. return undefined method empty?
if model.compiled_this_week.all.empty?
OR
if model.compiled_this_week.find(:all).empty?
.. work as expected
compiled_this_week is a named scope applied to specific date/search
conditions.
named_scope :compiled_this_week, lambda { { :conditions =>
[''created_at> ? and created_at < ?'', Time.now.beginning_of_week,
Time.now.end_of_week] } }
Any reason why this returns undefined method?
--
Posted via http://www.ruby-forum.com/.
Craig Demyanovich
2009-Jul-15 13:53 UTC
Re: undefined method empty? when find first invoked on db
As a named_scope, compiled_this_week will return a collection, even if it finds only one record matching its criteria. On a collection, #empty? will work. When you call #first or #find(:first) on the collection returned by compiled_this_week, you get the first record. #empty? is undefined on that record. Regards, Craig -- Craig Demyanovich Mutually Human Software http://mutuallyhuman.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 -~----------~----~----~----~------~----~------~--~---