I have list boxes and model relationships in the database. I'm doing the following migrations to maintain lists, rename items, and add items:

  def up
    Fund.create min: 0, max: 1_000_000
    add_column :sectors, :order, :integer, default: 0
    Sector.create name: 'Agriculture', order: 1
    Sector.create name: 'Construction', order: 2
    Sector.find( 9).update! name: 'Consumer Products', order: 3  

This will work until I must rollback a migration, then the primary keys for new rows in development will no longer match the ones in staging or production. I thought about installing ActiveAdmin, but then I would have to manually change 3 different databases (dev/staging/prod). Both ways seem tedious. I thought about #delete_all but there are foreign key constraints, and there are already users using the lists in production. Is there a Rails convention to update and add to lists in the database?

Oh yeah, I've also tried created lists in the code to more easily maintain them, like

class Ownership
  include ActiveModel::Model
  attr_accessor :id, :name

  def self.all
      { id: 1, name: 'Individual' },
   ].map { |v| Ownership.new v }

  def self.find_by_id(id)
    Ownership.all.select do |v|
      v.id == id

But then whenever I have to print one of the values, I have to look it up by id first. So instead of @record.ownership.name I have to use @ownership = Ownership.find_by_id(@record.ownership_id); @ownership.name;.

Oh yeah, I've also done this, when really pressed for time, which is the worst:

User Model:
  @@list_options = %w[0 1-5 6-10 10+]
  def self.list_options

User View:
  =f.select :field_1yr, User.list_options.map.with_index{|x,i| [x,i]}
  • From my point of view, if you create entities in migration rolling it back should delete them regardless of foreign key constraints (make sure that related entities are also deleted/adjusted). However, I don't know how this all is related to the concept of a list, can you clarify?
    – larsbe
    Sep 21, 2017 at 8:39
  • Yes the ones I just created are easily deleted, but the old ones that are used gives errors like 'foreign key constraint id 1 is in use by users table' (or similar).
    – Chloe
    Sep 21, 2017 at 16:07
  • Why do your primary keys in production need to match those in development? It's not like you're going to have a foreign key from a production table referenced by a row in a development table. Also, you should be able to write a custom 'down' migration that undoes exactly what your 'up' migration does, to enable rolling back properly. Oct 23, 2017 at 9:23
  • @SeanBurton They don't have to, but it's really nice and helpful (and OCD compliant) when they do. That way you can write queries without order by, hardcode the primary key in the code for comparisons, and it's easier to maintain and update referencing the id only.
    – Chloe
    Oct 25, 2017 at 17:04
  • If you're hardcoding primary keys in your code then you are doing things terribly wrong. It sounds like what you want is a separate indexed enum column... Oct 25, 2017 at 17:11

1 Answer 1


I had to do the following. It's just as tedious, but at least it doesn't depend on the primary key.

Sector.create name: 'Real Estate - Commercial', order: 20 
Sector.find_by_name('Real Estate (Residential/Commercial)').update! name: 'Real Estate - Residential', order: 21

I like this way best, to use a short, fixed, unique code to reference the row, and have an order column so sort by. Although ideally order will be by increments of 10's, to allow insertions without renumbering everything.

 id |      code      | order |                             name                             |         created_at         |         updated_at
  1 | finance        |     1 | Financial Services                                           | 2017-12-17 04:11:02.818388 | 2017-12-17 04:11:02.818388
  2 | energy         |     2 | Energy                                                       | 2017-12-17 04:11:02.823391 | 2017-12-17 04:11:02.823391
  3 | waste          |     3 | Waste to Energy Technologies                                 | 2017-12-17 04:11:02.828391 | 2017-12-17 04:11:02.828391

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.