2015-06-30 51 views
1

我有两个活动记录数组。我合并了这两个数组,现在我想根据“updated_at”字段对它们进行排序。未定义的方法`updated_at'为#<MatchResult :: ActiveRecord_Relation:0x0000000232e608> in rails

@notification_challenges=Challenge.where("to_id=? and activity_id=?" ,current_user.id,@activity.id) 
@match_results=[] 
@notification_challenges.each do |k| 
@[email protected]_results<<MatchResult.where("challenge_id=? and result_confirmation_status=?" ,k.id,1) 
end 

if [email protected]_results.blank? 
    @[email protected][email protected]_results 
end 

if [email protected]_challenges.blank? 
     @[email protected][email protected]_challenges 
end 

if [email protected]? 
     @notifications2=(@notifications).sort_by(&:updated_at) 
     @[email protected] 
end 

,但它给以下错误:

undefined method `updated_at' for #<MatchResult::ActiveRecord_Relation:0x0000000232e608> 

@notifications=(@[email protected]_challenges).sort_by(&:updated_at) 

工作正常。 但我想检查@match_results或@notification_challenges是否为空,所以我合并这两个数组,然后应用“sort_by”函数。

+0

使用此代码'@ notifications2 =(@ notifications).sort_by {| e | E [:的updated_at]}' –

回答

2

,你的情况可能MatchResult返回MatchResult对象与在它记录的数组,所以当你添加MatchResult你的第一个阵列到头来你会为这样的:

[a, b, c, MatchResult[d, e, f]]

其中此数组中的第4个元素是MatchResult对象,它没有update_at属性或方法。

你可以通过MatchResult结果尝试循环和推动每一个为第一阵列:

results = MatchResult.where("challenge_id=? and result_confirmation_status=?" ,k.id,1) 
@match_results = results.map{|c| @match_results << c} 

只是一个想法,因为我不知道什么MatchResult回报你的情况。

1
MatchResult.where("challenge_id=? and result_confirmation_status=?" ,k.id,1) 

已经返回一个数组(坦率地说是一个ActiveRecord :: Relation)。所以,不应该推。你应该加入。

我觉得最好还是将@notification_challenges初始化为一个空数组。以防万一...

@notification_challenges=[] 
@notification_challenges=Challenge.where("to_id=? and activity_id=?" ,current_user.id,@activity.id) 
@match_results=[] 
@notification_challenges.each do |k| 
@[email protected]_results + MatchResult.where("challenge_id=? and result_confirmation_status=?" ,k.id,1) 
end 
相关问题