When record is created, it has an auto-generated id. I want to get it.
I created a table of names in Ruby On Rails.
I entered four names:
- Alon
- Bil
- Hilary
- Erez
In my table, there is an auto id, right? The name ‘Alon’ is assigned id=1, and so on – the name ‘Erez’ with id=4. this Id is not an id that I defined in my table.
I explain with another words:
When I enter the function ‘Show’ for the name ‘Alon’, it looks like: http://localhost:3000/mycontroller/1. (I think that ‘1’ is the id of where the name ‘Alon’ is found in my table.
How can I get this id?
def index
@name = Tablenames.find(:all, :conditions => ["name=?", "Alon"]);
end
index.html.erb:
<% @name.each do |worker_name| %> worker_name.id <% end %>
When do you need the ID? Directly after creation?
When you’re searching for the name?