为什么uniq!方法有效但排序!不在 Rails 的这个数组上?
这是我创建数组的方法:
@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>
Here is how I created an array:
@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!
However, I want it to be in alphabetical order, so I tried to add .sort! and I got an error saying <=> method doesn't exist.
undefined method `<=>' for #<Company:0x9d506a8>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
排序公司并不“有效”。该错误消息意味着无法仅比较(在本例中使用比较运算符
<=>
)两家公司,因为它不知道您要比较什么:数据库中的 ID、名称、Ruby 内存中的 ID 等。不过,您可以自己定义排序行为:
Sorting companies doesn't "just work". What that error message means is that there is no way to just compare (use the comparison operator
<=>
, in this case) two companies, since it doesn't know what you would compare: ID in database, name, ID in Ruby memory, etc.You can define sorting behavior yourself, though: