Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 将ActiveRecord::Relation转换为可用数组_Ruby On Rails_Ruby_Arrays_Activerecord - Fatal编程技术网

Ruby on rails 将ActiveRecord::Relation转换为可用数组

Ruby on rails 将ActiveRecord::Relation转换为可用数组,ruby-on-rails,ruby,arrays,activerecord,Ruby On Rails,Ruby,Arrays,Activerecord,当我打电话时: preivous_lessons = @item.where("track_id = ?", lesson.track_id) 我得到这个活动记录不动产: [#,#] 如何将其转换为可用的模型数组,以便执行以下操作: preivous_lessons.each do |i| highest = i.order if i.order > highest end 正如OP从我的邮件中确认的那样,我的提示解决了他的问题,我把它作为对帖子的回答: preivous_less

当我打电话时:

preivous_lessons = @item.where("track_id = ?", lesson.track_id)
我得到这个活动记录不动产:

[#,#]

如何将其转换为可用的模型数组,以便执行以下操作:

preivous_lessons.each do |i|
  highest = i.order if i.order > highest
end
正如OP从我的邮件中确认的那样,我的提示解决了他的问题,我把它作为对帖子的回答:

preivous_lessons = @item.where("track_id = ?", lesson.track_id)
highest = preivous_lessons.maximum(:order)
文件:

计算给定列上的最大值。返回的值与列的数据类型相同,如果没有行,则返回
nil


highest=preivous\u lessons.map(&:order).max
highest=preivous\u lessons.max(:order)
@ArupRakshit进行操作然后我得到了这个错误
将NilClass与1个失败的比较
您是否尝试了
@item.where(“track\u id=?”,less.track\u id).最大(:order)
?得到了它的工作,这个错误是一个无关的问题@ArupRakshit post and answer,我将选择它作为顶部答案。
preivous_lessons = @item.where("track_id = ?", lesson.track_id)
highest = preivous_lessons.maximum(:order)