我正在使用Ruby on Rails 3,我想解決計數數組中ActiveRecord實例的問題。計數數組中的ActiveRecord實例時遇到問題
我有這樣的代碼
data = Account.where({:name => "Test_name", :city => "Test_city"}).limit(10)
的data
調試是
#<Account:0x000001029d2da0>#<Account:0x000001029d2c60>#<Account:0x000001029d2bc0>#<Account:0x000001029d2b20>
的data
檢查是
"[#<Account name: \"Test_name\", city: \"Test_city\">, #<Account … >, #<Account id… >, …]"
懷疑:## < ...>應該是類似#<Account...>,#<Account...>,<...>
(注意逗號)?
如果在我的代碼我用下面的
data_count = data.count
的data_count
是
nil
爲什麼nil
?我應該如何計算帳戶?
- 如果我使用
result = data.class
的result
調試是nil
,但如果我用result = data.class
調試是"{\"inheritable_attributes\":{}}"
。 - 如果我使用
Account.find_by_name("Test_name")
而不是Account.where(...)
,我會得到與上面相同的結果。
我無法複製這個。 – 2011-03-11 06:51:10
@安德魯馬歇爾爲什麼? : - \ – user502052 2011-03-11 06:55:40
當我在我自己的項目中的某個模型上完成這項工作時,我從'count'中得到預期結果。請在Rails控制檯中運行您的代碼,並從那裏準確地發佈您的會話。 – 2011-03-11 07:00:11