a newbie questionadding database field to a model basing only on schema (without model regeneration)

hi!

i am trying to add database field to a model without model regeneration. i have added my field to migration scripts

class CreateUsers < ActiveRecord::Migration   def change     create_table :users do |t|       t.string :name       t.string :lg, :default => "en"       t.timestamps     end   end end

field :lg

and now need to add this field as a property of "user" model how should i do it by hands, just adding a line of code, without model regenaration

hi!

i am trying to add database field to a model without model regeneration. i have added my field to migration scripts

class CreateUsers < ActiveRecord::Migration   def change     create_table :users do |t|       t.string :name       t.string :lg, :default => "en"       t.timestamps     end   end end

field :lg

and now need to add this field as a property of "user" model how should i do it by hands, just adding a line of code, without model regenaration

I don't know what you mean by model regeneration, but if you just want to add a field to an existing table you should create a new migration to add that field. You should never change the code in a migration that has already been run.

Colin

thanks for an advise not to change the code in migration tables. i could delete it and add new fields by new migrations scripts but these migration scripts have an influence only on a database schema, not on a table model in app/models catalogue.

the only thing i want to do is to add database field as a property to an existing model by hands, without generating scaffold

so the question is how to do it?

Please remember to quote the previous message and insert your reply inline at appropriate points in the previous message. Remember this is a mailing list not a forum (though you may be accessing it using a forum-like interface). Thanks.

thanks for an advise not to change the code in migration tables. i could delete it and add new fields by new migrations scripts but these migration scripts have an influence only on a database schema, not on a table model in app/models catalogue.

No you must not edit or delete an existing migration.

the only thing i want to do is to add database field as a property to an existing model by hands, without generating scaffold

A migration is nothing to do with a scaffold. As I said just generate a new migration to add the database field. That is exactly what migrations are for.

rails generate migration AddLgToUsers lg:string

Which will even include the add_column for you, though you will have to add the default yourself before running the migration. Have a look at the rails guide on migrations.

I guess that you are a beginner with rails and strongly recommend that you work right through a good tutorial such as railstutorial.org which will show you the basics of rails.

Colin

Thank you, Colin!

Colin Law wrote in post #1118501:

Please remember to quote the previous message and insert your reply inline at appropriate points in the previous message. Remember this is a mailing list not a forum (though you may be accessing it using a forum-like interface). Thanks.

OK

No you must not edit or delete an existing migration.

OK

the only thing i want to do is to add database field as a property to an existing model by hands, without generating scaffold

A migration is nothing to do with a scaffold. As I said just generate a new migration to add the database field. That is exactly what migrations are for.

I know. But the model should contain database fields definitions.

rails generate migration AddLgToUsers lg:string

Which will even include the add_column for you, though you will have to add the default yourself before running the migration. Have a look at the rails guide on migrations.

I think I should try with an add_column expression. Starting to search available online documentation.

I guess that you are a beginner with rails and strongly recommend that you work right through a good tutorial such as railstutorial.org which will show you the basics of rails.

Colin

Yes, I'm a beginner with rails and trying to understand how it works. Although I do not much trust generated code and prefer hand-written, it is possible it will work fine in special circumstances.

Valentin.

Sorry, it is not a solution. add_column in generated migration scripts adds desired column to database schema. Except that I need a property in my model of a table corresponding to a newly migrated column. That is the question.

? Rails models automatically derive from the current schema. If you're actually asking what I think you're asking, the answer is that you have to do absolutely nothing.

If you're curious, read up on the Active Record (two words) pattern here: P of EAA: Active Record

Each time you get an object, that object is built up from whatever is in the database record that backs it as of that moment. You either get the default value for that column/attribute, or you get the persisted value.

Walter

Actually, this is a better write-up, forget what I suggested earlier, that's a stub to a book you'd need to buy. (And I think everyone should own it, too.)

Walter

Firstly, somehow you are starting a new thread (as I view it in gmail at least) for every reply. I don't use ruby-forum so I don't know what you are doing wrong but something is not right. Have you considered signing up to the mailing list, then life will be much simpler.

Thank you, Colin!

Colin Law wrote in post #1118501:

Please remember to quote the previous message and insert your reply inline at appropriate points in the previous message. Remember this is a mailing list not a forum (though you may be accessing it using a forum-like interface). Thanks.

OK

No you must not edit or delete an existing migration.

OK

the only thing i want to do is to add database field as a property to an existing model by hands, without generating scaffold

A migration is nothing to do with a scaffold. As I said just generate a new migration to add the database field. That is exactly what migrations are for.

I know. But the model should contain database fields definitions.

Why? Models do not normally need to contain field definitions, rails picks them up automatically by interrogating the database.

rails generate migration AddLgToUsers lg:string

Which will even include the add_column for you, though you will have to add the default yourself before running the migration. Have a look at the rails guide on migrations.

I think I should try with an add_column expression. Starting to search available online documentation.

I guess that you are a beginner with rails and strongly recommend that you work right through a good tutorial such as railstutorial.org which will show you the basics of rails.

Colin

Yes, I'm a beginner with rails and trying to understand how it works. Although I do not much trust generated code and prefer hand-written, it is possible it will work fine in special circumstances.

It is best to use the generator to create a migration rb file so that the timestamp will be included in the file name. You can use the generator to create an empty migration then fill it in by hand if you prefer.

You definitely need to work right through a good tutorial.

Colin

Thank you all for the answers!

I only wished to improve I do not have to generate my model again after database schema migration. One person gave mi an advise to read that article

on migrations. The next step is generating scaffold for model, controller and view. But they are already generated and I do not want to loose their code applying my changes. As I see now there is no way of not regenerating model. Earlier I just hoped it could be possible to add a piece of hand-written code to my model to access newly added column in the last database migration. I heavily read documentation on ruby. Thank everybody for the answers.

you are starting a new thread (as I view it in gmail at least) for every reply.

I just use ruby-forum interface.

As I see now there is no way of not regenerating model.

No, there is absolutely no need to re-generate your model. *That* is what people are telling you.

Earlier I just hoped it could be possible to add a piece of hand-written code to my model to access newly added column in the last database migration.

You can do exactly that. It is also likely that you don't even need to.

- Simple access to the column comes for free. You don't need to do anything.

- Of course if you need to use that column within some larger function of the model, you will edit the model to add that. But you will *not* need to edit it to add simple accessors to get/set the column, those come for free and you will just use them.

But before doing anything else work through railstutorial.org or similar, as I have suggested several times I think. Otherwise in the future you will look back at your requests for help here and wish to die from embarrassment.

Colin