0

I have a query something like this in my controller that I am passing it as JSON to JBuilder,etc.... But JBuilder shows nothing! it just shows { }

@students =  Students.top_students.joins(:program).where("programs.organization_id = ?", params[:id]).limit(10)

So before it gets to there I just wanted to make sure it is getting the correct data, its joins are correct, etc.. So can I do that?

If I just say puts @students it will print something like, I want to see what is inside each of them.

#<Student:0x007fd33ebe57a8>
#<Student:0x007fd33ebe4d08>
#<Student:0x007fd33ebe3ea8>
4

1 回答 1

1

定义一个to_s方法Student,你的puts输出会更有用。我建议使用inspect

def to_s
  inspect
end

或者,您可以迭代@students并执行更多输出:

@students.each do |student|
  puts "Id: #{student.id}"
  puts "Name: #{student.name}"
  ...
end

或者,如果您想保持简单,快速collect就可以了:

puts @students.collect(&:inspect)
于 2013-03-06T22:35:57.540 回答