Question

I have a scaffold form Student with some fields like first_name, last_name, DOB, GPA, etc. and I want to be able to reference their database ID once they've been created and wondering what the best way to do this is. The migration class is called CreateStudents and the table of them is called :students so is there a way to reference the element of the students table as I iterate through it and display info for each student within it? By the way I'm using postgresql with rails 4.0.3 and ruby 2.0.0 if these make any difference. I'm able to use :students.each within my index html file but not sure how I can reference their ID number within the database.

Thanks

Was it helpful?

Solution

You should only do student.id. Even if you don't see the id field (or other field) in the Student.rb, if it exists in the database, you can access to it with the field name.

In a way of example i'm attaching an image of something like that but in rails console and my entity is called user

Example Image

OTHER TIPS

Assuming that you want to display the id field of students in index.html.erb:

<% @students.each do |student| %>

   <%= student.id %>  ## Display the id as this
    ......
<% end %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top