Starting with rails 2, for scaffold generation you are forced to pass the column name and data type of each field you want generated. This is fine for new tables, but if you have a table or have added dozens of columns to a table and want to regenerate, and you are lazy like me, typing this long list of columns is a pain. So this script will generate the input you need to pass the generator.
For example, you have a table called "resellers" and it has a bunch of columns, you could do:
ruby script/generate scaffold reseller first_name:string last_name:string address:string city:string ........ and keep on typing
or you can do:
ruby script/console
name = 'Resellers'
si_table_name = 'Resellers'
si_field_names = Array.new
si_cols = ActiveRecord::Base.connection.columns(si_table_name,
"#{name} Columns")
si_cols.each do |c|
si_field_names << "#{c.name}:#{c.type}"
end
puts si_field_names.join(' ')
It prints:
id:integer first_name:string last_name:string address:string city:string state:string postal_code:string country:string ....
now with the magic of cut and paste you can generate the scaffolding how you want without typing all this in.
Recent Comments