这里是我的末日代码NoMethodError:未定义的方法“ - @”
def self.sort_by_date_or_price(items, sort_by, sort_direction)
if sort_by == :price
items.sort_by{|x| sort_direction == :asc ? x.item.current_price : -x.item.current_price}
elsif sort_by == :date
items.sort_by{|x| sort_direction == :asc ? x.created_date : -x.created_date}
end
end
当我把这个方法#sort_by_date_or_price(items, :date, :desc)
它返回的NoMethodError: undefined method '[email protected]' for 2013-02-05 02:43:48 +0200:Time
如何解决这个错误?
你需要写一元运算符'-'该方法。这是不存在的,因此从'-x.item.current_price'或'-x.created_date'行确定出错。 – 2013-03-17 13:37:19
@iAmRubuuu,这段代码工作'items.sort_by {| x | -x.created_date}' – 2013-03-17 13:46:24