开发者

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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜