我的應用程序有幾個報告,我試圖爲所有這些集合製作group_by的輔助方法。Rails - group_by
例子:
def group_collection(collection, options = {})
column = options[:column]
group_count = collection.group_by{ |item| item.column.strftime('%b %y')}
end
這是我打算如何使用它
@user_groups = group_collection(@users, :column => "created_at")
不幸的是,這是行不通的。
undefined method `column' for... [CollectionObject]
如何,因此認爲自己的ActiveRecord列,而不是一個實例方法,使「列」變量的實際列類型在運行時任何線索?
謝謝。這工作。你在代碼中看到了哪些其他問題?我將不得不再等待7分鐘以將此標記爲已回答。 :) – AMIT 2011-02-09 19:02:21