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..

3
  • gives you an empty model? what does that mean? What are you trying to do? :) Commented Nov 7, 2010 at 21:02
  • i expected it to contain all fields and relations of that table, but all i get is an empty class that derives from ActiveRecord::Base Commented Nov 7, 2010 at 21:04
  • what you are doing is not possible unless you create your own scripts. Commented Nov 7, 2010 at 23:22

5 Answers 5

15

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.
11

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.

2 Comments

Wow, thanks I never knew this could happen! ehhe. I learn something new today.
THe generated rails models show your fields in the attr_accessible call.
3

Your answer is:

$ rake db:schema:dump

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

Comments

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.

Comments

1

Could try Magic Model Generator

2 Comments

Note: Is for Rails 2, no Rails 3 available.
Does anyone know of any alternative methods that generate models with relationships, etc from existing schema in Rails 3?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.