5

Is it possible to create controllers, models and view from the existing database?

I could not find the command over googling.

Here i am talking about Reverse Engineering

tereško
  • 58,060
  • 25
  • 98
  • 150
Neeraj
  • 8,625
  • 18
  • 60
  • 89
  • Just curious, if you are using `ruby on rails` then why do you want to create from database and why not from `rails command line`? – sjain Jan 24 '13 at 10:04
  • I would like to know the command to do this either from railscommand line. Can you provide that? – Neeraj Jan 24 '13 at 10:05

3 Answers3

2

You have to create simple model for every table with relations, and then you can

[rails3] > rails generate scaffold_controller Club name:string exclusive:boolean
      create  app/controllers/clubs_controller.rb
      invoke  erb
      create    app/views/clubs
      create    app/views/clubs/index.html.erb
      create    app/views/clubs/edit.html.erb
      create    app/views/clubs/show.html.erb
      create    app/views/clubs/new.html.erb
      create    app/views/clubs/_form.html.erb
      create    app/views/layouts/clubs.html.erb
      invoke  test_unit
      create    test/functional/clubs_controller_test.rb

Alternatively you can try active_admin gem

ActiveAdmin - https://github.com/gregbell/active_admin

rails generate active_admin:resource [MyModelName] 

RailsAdmin is also good enough https://github.com/sferik/rails_admin

You should specify at least 2 rules for your model if it doesn't use rails conventions. Example

class Article < ActiveRecord::Base
  self.table_name "tbl_articles"
  self.primary_key "art_id"
end
Fivell
  • 11,829
  • 3
  • 61
  • 99
  • does active_admin generate controller, models and view of that particular table [resource] with the default methods? – Neeraj Jan 24 '13 at 10:30
  • Yes, except models, you should define models by yourself with needed associations, logic, validation rules, etc... – Fivell Jan 24 '13 at 10:33
  • You see database table is not enough to create model automatically. – Fivell Jan 24 '13 at 10:35
  • hmm... I thought Active Record Pattern should be capable of doing that like other ORM (Doctrine) – Neeraj Jan 24 '13 at 10:36
0

This is how you can do that -

Try:

rails g scaffold myscaffold

This will generate the files:

invoke  active_record
create    db/migrate/20130124100759_create_myscaffolds.rb
create    app/models/myscaffold.rb
invoke    test_unit
create      test/unit/myscaffold_test.rb
create      test/fixtures/myscaffolds.yml
 route  resources :myscaffolds
invoke  scaffold_controller
create    app/controllers/myscaffolds_controller.rb
invoke    erb
create      app/views/myscaffolds
create      app/views/myscaffolds/index.html.erb
create      app/views/myscaffolds/edit.html.erb
create      app/views/myscaffolds/show.html.erb
create      app/views/myscaffolds/new.html.erb
create      app/views/myscaffolds/_form.html.erb
invoke    test_unit
create      test/functional/myscaffolds_controller_test.rb
invoke    helper
create      app/helpers/myscaffolds_helper.rb
invoke      test_unit
create        test/unit/helpers/myscaffolds_helper_test.rb
invoke  assets

invoke    coffee
create      app/assets/javascripts/myscaffolds.js.coffee
invoke    scss
create      app/assets/stylesheets/myscaffolds.css.scss
invoke  scss
identical    app/assets/stylesheets/scaffolds.css.scss
sjain
  • 23,126
  • 28
  • 107
  • 185
  • I don't think the above command will read my database and crate controllers, models and views of the same using scaffolding...would it? – Neeraj Jan 24 '13 at 10:10
  • why not? There is a file `config/database.yml` and you are giving your database(current) options over there. – sjain Jan 24 '13 at 10:12
  • For example i have 10 tables in my database and i execute the above command then are you sure that MVC structure will be created for all 10 tables? – Neeraj Jan 24 '13 at 10:16
  • And what about the ORM?, you can access the database, but you need the objects mapped. – Paulo Fidalgo Jan 24 '13 at 10:17
  • there is nothing related to the other tables in the above command as it is just for creating the new scaffold `myscaffold` in the current database. If you want to update others then use migrations for corresponding tables. – sjain Jan 24 '13 at 10:19
  • Saurabh: now you got what i was pointing to (reverse engineering). It seems that reverse engineering is not available in RoR as per Paulo Fidalgo comment. – Neeraj Jan 24 '13 at 10:21
-1

Well this goes against principles. The better you have to do, if you want a quick bootstrap for your application is replicate the models you have on your database and use scaffolding. Remember that Rails use a LOT of conventions, and if you decide not follow you'll have a lot of trouble.

Check this guide if you need help.

Paulo Fidalgo
  • 21,709
  • 7
  • 99
  • 115
  • My concerns was just to know if that is possible otherwise generating models, controllers and views using scaffolding is fine. dont mind :) – Neeraj Jan 24 '13 at 10:12