On the following query I get objects as return value (and thus checking for no results using .nil? works just fine:
store = Store.where(:some_id => myobj.some_id.to_i).first
unless store.nil?
But on this next code I get return type of ActiveRecord::Relation and checking using .nil? fails!
existing_store = Store.where(:some_str => myobj.some_str).not_hidden().active()
if existing_store.nil?
1. Why is that?
2. How can I check for no results in this case?
3. How could I check for no records return in general when using find() or where()
Rails 3.1 has really made this confusing.
use
blank?,nil?is true, if it’s reallynil(single instance ofNilClass), but your second example always will return an Array, maybe empty, if there are no results, but an Array nonetheless.blank?checks for empty arrays, empty strings, nil and false values.If you have problems with
blank?not behaving as expected you can check forfirst.nil?