`

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
分享到:
评论

相关推荐

    Rails.5.Revealed.For.those.Upgrading.to.Version.5

    This short early adopter book details both how you'll upgrade existing web and other applications from Rails 4.2 to 5 and how to create new applications in Rails 5. The headline features, Rails API ...

    The Rails 5 Way-Leanpub(2017).pdf

    Since the API documentation is liberally licensed (just like the rest of Rails), there are some sections of the book that draw from the API documentation. But in practically all of those cases, the ...

    Bootstrap for Rails (2015)

    Summary 94 Chapter 6: Creating Navigation Bars 95 Getting started with a navigation bar 95 Navigation bar helper classes 102 Adding a navigation bar to the Rails application 103 Summary 105 Chapter 7:...

    The Rails 3 Way(2nd)

    Ruby on Rails strips complexity from the development process, enabling professional developers to focus on what matters most: delivering business value via clean and maintainable code. The Rails™ 3 ...

    Rails 5 Revealed(Apress,2016)

    This short early adopter book details both how you’ll upgrade existing web and other applications from Rails 4.2 to 5 and how to create new applications in Rails 5. The headline features, Rails API ...

    The Rails3 Way, 2nd Edition

    Ruby on Rails strips complexity from the development process, enabling professional developers to focus on what matters most: delivering business value via clean and maintainable code. The Rails™ 3 ...

    Learning Rails.pdf

    this unique book approaches Rails development from the outer layer: the application interface. You'll learn how to create something visible with Rails before reaching the more difficult database ...

    graphql-rails-generators:Graphql Rails Scaffold:trade_mark:。 从Rails模型自动生成GraphQL类型

    graphql-rails-generators 一些生成器可以轻松将Rails模型与集成。 我之所以创建它,是因为我浪费了太多的按键来手动复制模型架构以创建graphql类型。 该项目包含用于查看ActiveRecord模型架构的生成器,并为您...

    Rails for .NET Developers

    not just Rails but all the open source technologies that go along with it. This book will be your guide as you navigate this new terrain. For a .NET developer, learning Rails is as much about the ...

    Rails Plugins:Extending Rails Beyond the Core

    Rails Plugins:Extending Rails Beyond the Core

    Rails.Angular.Postgres.and.Bootstrap.2nd.Edition

    Embrace the full stack of web development, from styling with Bootstrap, building an interactive user interface with Angular 2, to storing data quickly and reliably in PostgreSQL. With this fully ...

    the rails way

    rails 2.0 english version

    ruby rails demo

    ruby rails demo, rails 简单demo。 (1)到ruby官网:http://www.ruby-lang.org/en/下载window安装包,并安装, ruby版本: ruby -v (2)安装Rails3 gem install rails (3)安装sqlite3 gem install sqlite3-ruby 安装...

    Ajax on Rails

    For those new to Rails, this book provides a quick introduction, the big picture, a walk through the installation process, and some tips on getting started. If you've already started working with ...

    The Rails 4 Way

    学习ruby!

    Learning Rails 5(高清文字pdf版)

    Rather than toss you into the middle of the framework’s Model-View-Controller architecture, as many books do, Learning Rails 5 begins with the foundations of the Web you already know. You’ll learn ...

    The Rails 5 way 英文原版

    The Rails way 最新版本 The Rails way 最新版本 The Rails way 最新版本

    Agile Web Development with Rails 4

    You concentrate on creating the application, and Rails takes care of the details., Tens of thousands of developers have used this award-winning book to learn Rails. It’s a broad, far-reaching ...

    rails6_scaffold:Ruby on Rails 6.0,Webpack,Bootstrap 4.3和Font Awesome入门应用程序

    这将为您的项目创建一个新文件夹,请确保在所有文件中搜索Rails6Scaffold和rails6_scaffold ,因为在某些地方您需要手动更改名称。 这是找到的文件列表,在运行“重命名” gem后需要手动更新: app/views/layouts...

    [Rails] Crafting Rails Applications (英文版)

    This pioneering book is the first resource that deep dives into the new Rails 3 APIs and shows you how use them to write better web applications and make your day-to-day work with Rails more ...

Global site tag (gtag.js) - Google Analytics