accessing data from the model using script/console (Ruby on Rails)
I'm looking to write a Ruby script which I can load into the Rails Consol开发者_StackOverflow中文版e 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
students = Student.all
students.each do |student|
p student.name
end
This will do what you want:
Student.all.collect(&:name)
- You can find an explanation of the syntax in this Railscast.
精彩评论