Active Record: Can it auto-create database tables for you?

Hi,

Just get started with Rails and I’m trying to read ahead and find out
whether Active Record supports auto-creation of database tables for you?

Is this supported, or is the concept that you write your own database
DDL to do this?

Thanks

Greg H. wrote on 15.07.2006 23:24:

Hi,

Just get started with Rails and I’m trying to read ahead and find out
whether Active Record supports auto-creation of database tables for you?

There is
http://api.rubyonrails.org/classes/ActiveRecord/Migration.html

It is not really auto-creation. You have to script it and do a “rake
migrate”. But the advantage is that it can handle versioning and changes
in your db layout.

You can also use AR to create tables programatically:
http://api.rubyonrails.org/classes/ActiveRecord/Schema.html

If you want to use all features of a specific DBMS you’ll better use the
DB tools to create a layout and do a schema export to SQL or
corresponding.

Markus

On Sun Jul 16, 2006 at 05:00:07AM +0200, Greg H. wrote:

thanks Markus

These tools you mention (ActiveRecord/Migration and ActiveRecordSchema)
seem to offer a non-SQL syntax for specifying your database tables.

I was wondering however whether Rails supported the concept of the
database being dynamcially generated based on the Model files (i.e.
following the Don’t Repeat Yourself principle).

no

you have to repeat yourself, and build a schema, and then build
something in your model files and hopes it matches.

thanks Markus

These tools you mention (ActiveRecord/Migration and ActiveRecordSchema)
seem to offer a non-SQL syntax for specifying your database tables.

I was wondering however whether Rails supported the concept of the
database being dynamcially generated based on the Model files (i.e.
following the Don’t Repeat Yourself principle).

That is, if the Model is already defined whether Rails could
auto-generate the initial database schema, and if there was a change to
the Model then create/manage the necessary changes to the database?
(model driven development concept to some extent)

Thanks

Greg H. wrote:

thanks Markus

These tools you mention (ActiveRecord/Migration and ActiveRecordSchema)
seem to offer a non-SQL syntax for specifying your database tables.

I was wondering however whether Rails supported the concept of the
database being dynamcially generated based on the Model files (i.e.
following the Don’t Repeat Yourself principle).

That is, if the Model is already defined whether Rails could
auto-generate the initial database schema, and if there was a change to
the Model then create/manage the necessary changes to the database?
(model driven development concept to some extent)

Thanks

Hi,
You seem to think that you define the DB table in the model. You don’t.
There is not enough in the model for AR to pull out and make a DBtable
of it. This is helpful, because it doesn’t restrain your model code to
a schema as tightly, so you can quick add a column using your favorite
tool and not worry about editing your model or dataloss.
Chris

On Sunday, July 16, 2006, at 10:45 AM, Greg H. wrote:

You seem to think that you define the DB table in the model. You don’t.
Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

The changes are reflected instantly.

Model files define the behavior of the model, not its definition. You
put things in the model file that define associations with other models,
validations, helper methods, etc. The database table holds the
definition of which ‘attributes’ are available, so if you change the
table, you automatically change what is available through the model.

_Kevin
www.sciwerks.com

Is there any way of generating all your models if you have created the
database however?

Thanks

Tim

On Sunday, July 16, 2006, at 4:09 PM, Tim P. wrote:

Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

ruby script/generate model ModelName

works pretty well.
You still have to manually define the associations.

_Kevin
www.sciwerks.com

Thanks

What’s the basic approach that AR uses to solve this? Does it
dynamically build up objects in memory? How does synchronisation
between the database tables (e.g. if they change) and the AR/rails
object occur? How quickly are database changes reflected in the
objects?

Chris C. wrote:

Hi,
You seem to think that you define the DB table in the model. You don’t.
There is not enough in the model for AR to pull out and make a DBtable
of it. This is helpful, because it doesn’t restrain your model code to
a schema as tightly, so you can quick add a column using your favorite
tool and not worry about editing your model or dataloss.
Chris

Greg H. wrote:

thanks - does this imply a general performance impact for rails? Is
there any level of caching that goes on by default in AR? i.e. does
every browser request effectively trigger AR to double check the latest
definition of the model via a request to the database to check whether
anything has changed?

Caching is by default turned on for this in production env. but not
development env.

thanks - does this imply a general performance impact for rails? Is
there any level of caching that goes on by default in AR? i.e. does
every browser request effectively trigger AR to double check the latest
definition of the model via a request to the database to check whether
anything has changed?

Kevin O. wrote:

Model files define the behavior of the model, not its definition. You
put things in the model file that define associations with other models,
validations, helper methods, etc. The database table holds the
definition of which ‘attributes’ are available, so if you change the
table, you automatically change what is available through the model.

I know that!! I was refering to (like i have just had) 30+ tables in a
database if there was a quick way of building all the models?

cheers

Tim

Kevin O. wrote:

On Sunday, July 16, 2006, at 4:09 PM, Tim P. wrote:

Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

ruby script/generate model ModelName

works pretty well.
You still have to manually define the associations.

_Kevin
www.sciwerks.com

On Monday, July 17, 2006, at 12:13 AM, Greg H. wrote:

definition of which ‘attributes’ are available, so if you change the
table, you automatically change what is available through the model.


Posted via http://www.ruby-forum.com/.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

AR doesn’t ‘check to see if anything has changed’, when you load a
model, it just creates it with an attribute corresponding to whatever
columns are defined at that time. To some extent, your db schema is the
model definition.

_Kevin
www.sciwerks.com

Yes there’s a way to generate models for all 30 tables in your schema.

ruby script/generate model People
ruby script/generate model Places

ruby script/generate model Things

If your database uses tables to store the metadata (I know Oracle does)
then it’s trivial to make a script that will generate the above
statements
from your metadata. There’s no features in ActiveRecord that will do
this
for you. Besides… the generator also creates unit tests files for
you.

30 tables, and I bet that you don’t want models for all of them, as
some
of them might be linking tables. If this is your first Rails project,
you
should stop right now and do some reading. Rails is a wonderful
framework
but if you choose to map a Rails app to an existing pre-Rails schema,
your
going to have nothing but headaches and questions unless you know Rails
well
enough to know the obstacles you’ll encounter and the methods you can
use to
overcome them. There are some things that exist in legacy shemas that
cannot
be dune with Rails.

On 17 Jul 2006 11:19:49 -0000, Kevin O. <

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs