在rails中查询错误数据

时间:2015-07-13 08:36:04

标签: ruby-on-rails ruby ruby-on-rails-4

我已经完成了这个

def show_selected_students(selected_students, students)   
    student = students.map{|a| a.name}
    selected_students = selected_students.split(",")
    @student_selected = selected_students.map {|i| student[i.to_i] }
end

在学生中我正在获取给定的数据

#<ActiveRecord::Relation [#<Student id: 1, name: "XYZ",>, #<Student id: 2,  name: "test1">, #<Student id: 3,  name: "cherry">, #<Student id: 4,  name: "mary">, #<Student id: 5,  name: "hary">, #<Student id: 35,  name: "hen">, #<Student id: 44,  name: "duck">, #<Student id: 62,  name: "try">]>

并且在selected_students中我得到了2,3,4现在我想要获取那些id与selected_students匹配的学生,因为我写了这个,但它给了我输出['cherry', 'mary', 'hary'],即id 3,4,5但我想要2,3,4请指导我如何解决这个问题。提前完成。

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

def show_selected_students(selected_students, students)       
  selected_students = selected_students.split(",")
  @student_selected = students.where(:id => selected_students).map{|a|a.name}
end