Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to DRY this migration up?

I have a table, folders that I want some other tables to reference, so far my migration script looks like this:

create_table :folders do |t|
  t.timestamps
end

....

change_table table1 do |t|
  t.references :folders
end
change_table table2 do |t|
  t.references :folders
end
change_table table3 do |t|
  t.references :folders
end
change_table table4 do |t|
  t.references :folders
end

Since I'm doing essentially the same thing to each table, is there a more concise and maintainable way to write this?

Thanks

like image 284
Chris Avatar asked Dec 16 '25 15:12

Chris


1 Answers

Try to do this:

[table1, table2, table3, table4].each do |tbl|
    change_table tbl { |t| t.references :folders }
end

I hope you don't name tables with pattern table{#n} and give them good names in your actual code :)

like image 154
Aliaksei Kliuchnikau Avatar answered Dec 19 '25 06:12

Aliaksei Kliuchnikau