這裏是我創建了一個數組:爲什麼uniq!方法的工作,但排序!不在這個數組在軌?
@companies_with_email = Company.contact_emails_date_sent_gt(@monday).
contact_emails_date_sent_lt(@friday).
find(:all, :select => "distinct companies.* ") || []
@companies_with_call = Company.contact_calls_date_sent_gt(@monday).
contact_calls_date_sent_lt(@friday).
find(:all, :select => "distinct companies.* ") || []
@companies_with_activity = @companies_with_email + @companies_with_call
@companies_with_activity.uniq!
不過,我希望它是按字母順序排列的,所以我試圖添加的.sort!我得到一個錯誤,說< =>方法不存在。
undefined method `<=>' for #<Company:0x9d506a8>
要回答在標題的問題:'uniq'和'sort'需要不同的距離的方法(「鴨子類型」)對象在集合/枚舉中。 'uniq'需要'hash'和'eql?','sort'需要'<=>'。 ActiveRecord :: Base提供'eql?'和'hash'(所以'uniq'作品),但它不提供'<=>'(所以'排序'不起作用)。 – 2010-10-20 03:05:17