Greg Hauptmann
2006-Jul-15 21:24 UTC
[Rails] 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 -- Posted via http://www.ruby-forum.com/.
Markus Kolb
2006-Jul-15 22:22 UTC
[Rails] Re: Active Record: Can it auto-create database tables for you?
Greg Hauptmann 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
Greg Hauptmann
2006-Jul-16 03:00 UTC
[Rails] Re: Active Record: Can it auto-create database tables for yo
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 -- Posted via http://www.ruby-forum.com/.
carmen
2006-Jul-16 03:08 UTC
[Rails] Re: Active Record: Can it auto-create database tables for yo
On Sun Jul 16, 2006 at 05:00:07AM +0200, Greg Hauptmann 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.> > 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 > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Chris Carter
2006-Jul-16 04:55 UTC
[Rails] Re: Active Record: Can it auto-create database tables for yo
Greg Hauptmann 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) > > ThanksHi, 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 -- Posted via http://www.ruby-forum.com/.
Greg Hauptmann
2006-Jul-16 08:45 UTC
[Rails] Re: Active Record: Can it auto-create database tables for yo
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 Carter 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-- Posted via http://www.ruby-forum.com/.
Kevin Olbrich
2006-Jul-16 12:39 UTC
[Rails] Re: Active Record: Can it auto-create database tables for yo
On Sunday, July 16, 2006, at 10:45 AM, Greg Hauptmann wrote:>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 Carter 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 > > >-- >Posted via http://www.ruby-forum.com/. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railsThe 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 -- Posted with http://DevLists.com. Sign up and save your mailbox.
Tim Perrett
2006-Jul-16 14:09 UTC
[Rails] Re: Active Record: Can it auto-create database tables for yo
Is there any way of generating all your models if you have created the database however? Thanks Tim -- Posted via http://www.ruby-forum.com/.
Kevin Olbrich
2006-Jul-16 19:27 UTC
[Rails] Re: Active Record: Can it auto-create database tables for yo
On Sunday, July 16, 2006, at 4:09 PM, Tim Perrett wrote:>Is there any way of generating all your models if you have created the >database however? > >Thanks > >Tim > >-- >Posted via http://www.ruby-forum.com/. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railsruby script/generate model ModelName works pretty well. You still have to manually define the associations. _Kevin www.sciwerks.com -- Posted with http://DevLists.com. Sign up and save your mailbox.
Greg Hauptmann
2006-Jul-16 22:14 UTC
[Rails] Re: Active Record: Can it auto-create database tables for yo
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 Olbrich 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.-- Posted via http://www.ruby-forum.com/.
Robert James
2006-Jul-17 00:52 UTC
[Rails] Re: Active Record: Can it auto-create database tables for yo
Greg Hauptmann 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. -- Posted via http://www.ruby-forum.com/.
Tim Perrett
2006-Jul-17 07:54 UTC
[Rails] Re: Active Record: Can it auto-create database tables for yo
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 Olbrich wrote:> On Sunday, July 16, 2006, at 4:09 PM, Tim Perrett wrote: >>Rails mailing list >>Rails@lists.rubyonrails.org >>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-- Posted via http://www.ruby-forum.com/.
Kevin Olbrich
2006-Jul-17 11:18 UTC
[Rails] Re: Active Record: Can it auto-create database tables for yo
On Monday, July 17, 2006, at 12:13 AM, Greg Hauptmann 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? > >Kevin Olbrich 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. > > > >-- >Posted via http://www.ruby-forum.com/. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railsAR 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 -- Posted with http://DevLists.com. Sign up and save your mailbox.
Brian Hogan
2006-Jul-17 12:41 UTC
[Rails] Re: Active Record: Can it auto-create database tables for yo
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 Olbrich < devlists-rubyonrails@devlists.com> wrote:> > > On Monday, July 17, 2006, at 12:13 AM, Greg Hauptmann 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? > > > >Kevin Olbrich 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. > > > > > > > >-- > >Posted via http://www.ruby-forum.com/. > >_______________________________________________ > >Rails mailing list > >Rails@lists.rubyonrails.org > >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 > > -- > Posted with http://DevLists.com. Sign up and save your mailbox. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060717/fc18e5bf/attachment.html