rails scaffold getting the column list from existing table

之前一直找这个问题的解决办法,但一直没找到,在g上看到了一个人发的博客,虽然不是理想的实现,但至少有人和我有一样的需求,欣慰!

博客内容:
rails scaffold getting the column list from existing table

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.

来源:http://jnylund.typepad.com/joels_blog/2010/03/rails-scaffold-getting-the-column-list-from-existing-table.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值