Posted By


ctran on 02/20/08

Tagged


Statistics


Viewed 341 times
Favorited by 1 user(s)

migration_helpers.rb


/ Published in: Ruby
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #---
  2. # Excerpted from "Advanced Rails Recipes",
  3. # published by The Pragmatic Bookshelf.
  4. # Copyrights apply to this code. It may not be used to create training material,
  5. # courses, books, articles, and the like. Contact us if you are in doubt.
  6. # We make no guarantees that this code is fit for any purpose.
  7. # Visit http://www.pragmaticprogrammer.com/titles/fr_arr for more book information.
  8. #---
  9. module MigrationHelpers
  10.  
  11. def fk(from_table, from_column, to_table)
  12. execute %(alter table #{from_table}
  13. add constraint #{constraint_name(from_table, from_column)}
  14. foreign key (#{from_column})
  15. references #{to_table}(id))
  16. end
  17.  
  18. def drop_fk(from_table, from_column)
  19. execute %(alter table #{from_table}
  20. drop foreign key #{constraint_name(from_table, from_column)})
  21. end
  22.  
  23. def constraint_name(table, column)
  24. "fk_#{table}_#{column}"
  25. end
  26.  
  27. end

URL: http://media.pragprog.com/titles/fr_arr/code/buffet/lib/migration_helpers.rb

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.