views:

124

answers:

2

I'm looking to write a Ruby script which I can load into the Rails Console which will access data from the models I have created. I created a model called student using the following command and populated it with data:

script/generate scaffold student given_name:string middle_name:string family_name:string date_of_birth:date grade_point_average:decimal start_date:date

how would I, for instance, get the script to print a list of names of all the students? are there any resources which describe how to do this in detail? I haven't been able to find anything. Thanks!

  • Steve
+1  A: 
students = Student.all
students.each do |student|
  p student.name
end
KJF
+2  A: 

This will do what you want:

Student.all.collect(&:name)
  • You can find an explanation of the syntax in this Railscast.
John Topley