Rails 3:How to Generate Models for Existing Database Tables

rails 3:how to generate models for existing database tables

A Rails model doesn't show your fields, but you can still use them. Try the following. Assuming you have a Model named ModelName and a field called "name", fire up the Rails console and type:

ModelName.find_by_name('foo')

Given a name that exists in the DB, you should see results.

Rails doesn't infer relationships though, but if your database follows Rails conventions they are easily added.

Update

I've noticed this particular lack of explicitness ("magic") is a source of confusion for newbies to Rails. You can always look in schema.rb to see the models and all the fields in one place. Also, if you would prefer to see the schema for each model in the model file, you can use the annotate_models gem, which will put the db schema in a comment at the top of the model file.

Generate models from existing tables using Rails 3

It's not that difficult, just takes a bit more configuration. Here's a basic template for a model:

class YourIdealModelName < ActiveRecord::Base
self.table_name = `actual_table_name`
self.primary_key = `ID`

belongs_to :other_ideal_model,
:foreign_key => 'foreign_key_on_other_table'

has_many :some_other_ideal_models,
:foreign_key => 'foreign_key_on_this_table',
:primary_key => 'primary_key_on_other_table'
end

Generate datamapper models from an existing database

At last, I found that till now the best solution is to use dm-is-reflective plugin: https://github.com/godfat/dm-is-reflective.

It doesn't generate the code for DataMapper models reflecting an existing database schema, but its properties access methods are automatically available (as long as you keep using this plugin, of course).

Here is an example of use:

require 'data_mapper'
require 'dm-is-reflective'

DataMapper.setup(:default, "postgres://user:pwd@localhost/db")

class Table
include DataMapper::Resource

is :reflective #activate dm-is-reflective

reflect #reflects eeach property. You can be more specific (look at plugin documentation)
end

DataMapper.finalize

#Even if no field is defined, all of them are accessible
entry = Table.first(nil, {:id => 469})
print entry.anotherField

Ruby On Rails: Create Models View And Controller from existing database

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

rails g scaffold for existing model and DB table

rails generate scaffold_controller Article

Rails Generate Model from Existing Table?

Rails is very opinionated, so if you have a table called "person" and you want the corresponding model to be called Person, you need to tell Rails explicitly not to be so clever (otherwise, it will assume that it needs to look for the plural of the model name for the table name).

class Person < ActiveRecord::Base
set_table_name 'person'
end

If your table's primary key isn't called "id", then you'll need to specify that, too...

set_primary_key 'person_id'

You may also need to specify a different autoincrement sequence name, depending on your database.

There's not a way that I know of to automatically generate a model from an existing legacy table, but this should get you most of the way there.



Related Topics



Leave a reply



Submit