rails 3:how to generate models for existing database tables
Asked Answered
S

6

18

I've configured my database.yml to point to my existing mysql database

how can I generate models from it?

rails generate model existing_table_name

only gives an emty model..

Smythe answered 7/11, 2010 at 20:58 Comment(3)
gives you an empty model? what does that mean? What are you trying to do? :)Webby
i expected it to contain all fields and relations of that table, but all i get is an empty class that derives from ActiveRecord::BaseSmythe
what you are doing is not possible unless you create your own scripts.Webby
D
10

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.

Difficult answered 8/11, 2010 at 0:36 Comment(2)
Wow, thanks I never knew this could happen! ehhe. I learn something new today.Karyotin
THe generated rails models show your fields in the attr_accessible call.Daw
W
15

You can try Rmre. It can create models for existing schema and it tries to create all relationships based on foreign keys information.

Winters answered 12/9, 2012 at 10:50 Comment(1)
I tried Rmre but it created only the relationships between the models (e.g. has_many, belongs_to). It did not create the model attributes.Unconcern
D
10

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.

Difficult answered 8/11, 2010 at 0:36 Comment(2)
Wow, thanks I never knew this could happen! ehhe. I learn something new today.Karyotin
THe generated rails models show your fields in the attr_accessible call.Daw
T
3

Your answer is:

$ rake db:schema:dump

That will set a new db/schema.db to create a schema of your DB.

Tunicate answered 22/3, 2014 at 15:9 Comment(0)
S
1

ActiveRecord doesn't parse a schema definition. It asks the DBM for the table defs and figures out the fields on the fly.

Having the schema is useful if you are going to modify the tables via migrations. Schema Dumping and You will help you dump it to use as a reference for building migrations.

ActiveRecord makes some suppositions about the table naming and expects an id field to be the primary key with a sequential number as the type. Having the migrations would help you to refactor the tables and/or fieldnames and types, but you can do those same things via your DBM's command-line. You don't really have to follow ActiveRecord's style but doing so helps avoid odd errors and lets AR infer things to make your life easier.

Seville answered 8/11, 2010 at 1:21 Comment(0)
M
1

Could try Magic Model Generator

Masakomasan answered 10/11, 2010 at 6:47 Comment(2)
Note: Is for Rails 2, no Rails 3 available.Devries
Does anyone know of any alternative methods that generate models with relationships, etc from existing schema in Rails 3?Caddis
T
0

Take a look at rare_map gem. https://github.com/wnameless/rare_map It works both on Rail 3 and 4.

Talkfest answered 27/2, 2014 at 14:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.