I'm on Ruby on Rails and I have a integer column called position
.
What's the best way to assign a position
value which is bigger than the the biggest on its table, when creating a new record?
At the moment i'm doing this:
# db/migrate/entry_migration
create_table :entries do |t|
t.integer :position
#...
end
# app/views/entries/_form
<%= simple_form_for @entry do |f| %>
<% if @entry.new_record? && @entries.order('position').last.present? %>
<%= f.input :position, as: :hidden, value: @entries.order('position').last.position + 1 %>
<% else %>
<%= f.input :position, as: :hidden %>
<% end %>
<% end %>