在下面的查詢我得到的對象作爲返回值(並因此檢查使用.nil沒有結果工作得很好:ActiveRecord ::關係問題檢查是否爲零? - Rails的3.1
store = Store.where(:some_id => myobj.some_id.to_i).first
unless store.nil?
但在這接下來的代碼中,我得到的返回類型? ActiveRecord的::關係,並檢查使用.nil?失敗!
existing_store = Store.where(:some_str => myobj.some_str).not_hidden().active()
if existing_store.nil?
1.這是爲什麼?
2.在這種情況下,我該如何檢查結果?
3.我怎麼能檢查是否有記錄,使用find()或者()
的Rails 3.1確實令這混亂的時候一般返回。
'not_hidden'和'active'的範圍我認爲不是問題的行爲?而調用'#nil?'究竟返回的是什麼? – tbuehlmann
是的,那些是範圍......並調用.nil?總是爲false,因爲返回值是一個空數組(因此從不nil) – Hopstream
正如下面已經建議的那樣,我會使用'#empty?'。 – tbuehlmann