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 }
end
def self.find_by_id(id)
Ownership.all.select do |v|
v.id == id
end
end
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
@@list_options
end
User View:
=f.select :field_1yr, User.list_options.map.with_index{|x,i| [x,i]}
order by
, hardcode the primary key in the code for comparisons, and it's easier to maintain and update referencing theid
only.