Wednesday, 12 September 2012

Create Model for all Table

Below code will fetch all table names and create model
 ActiveRecord::Base.connection.tables.each do |table|
     model_file = File.join("app", "models", table.singularize+".rb")     
     model_name = table.classify
     File.open(model_file, "w+") do |file|
      file << "class #{model_name} < ActiveRecord::Base\nend" 
     end
 end

No comments:

Post a Comment