Undefined method empty? when find first invoked on db

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?

Ah that makes sense now - thanks for the clarification on that Craig!

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 D.
Mutually Human Software
http://mutuallyhuman.com

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs