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

21 views
Skip to first unread message

Valentin Kotelnitski

unread,
Aug 12, 2013, 11:26:42 AM8/12/13
to rubyonra...@googlegroups.com
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

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

Colin Law

unread,
Aug 12, 2013, 11:38:50 AM8/12/13
to rubyonra...@googlegroups.com
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

>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/31f262b4a14461c3a3cab958a5e50c7e%40ruby-forum.com?hl=en-US.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Valentin Kotelnitski

unread,
Aug 12, 2013, 11:47:21 AM8/12/13
to rubyonra...@googlegroups.com
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?

Colin Law

unread,
Aug 12, 2013, 12:14:22 PM8/12/13
to rubyonra...@googlegroups.com
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.

On 12 August 2013 16:47, Valentin Kotelnitski <li...@ruby-forum.com> wrote:
> 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

Valentin Kotelnitski

unread,
Aug 12, 2013, 12:38:59 PM8/12/13
to rubyonra...@googlegroups.com
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.

Valentin Kotelnitski

unread,
Aug 12, 2013, 1:57:14 PM8/12/13
to rubyonra...@googlegroups.com
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.

Scott Ribe

unread,
Aug 12, 2013, 2:07:31 PM8/12/13
to rubyonra...@googlegroups.com
On Aug 12, 2013, at 11:57 AM, Valentin Kotelnitski <li...@ruby-forum.com> wrote:

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

--
Scott Ribe
scott...@elevated-dev.com
http://www.elevated-dev.com/
(303) 722-0567 voice




Walter Lee Davis

unread,
Aug 12, 2013, 2:37:17 PM8/12/13
to rubyonra...@googlegroups.com
If you're curious, read up on the Active Record (two words) pattern here: http://martinfowler.com/eaaCatalog/activeRecord.html

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
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/2BEB9011-44E6-4FB0-8E55-79129181B586%40elevated-dev.com?hl=en-US.

Walter Lee Davis

unread,
Aug 12, 2013, 2:38:55 PM8/12/13
to rubyonra...@googlegroups.com
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.)

https://en.wikipedia.org/wiki/Active_record_pattern

Walter

On Aug 12, 2013, at 2:07 PM, Scott Ribe wrote:

Colin Law

unread,
Aug 12, 2013, 4:12:15 PM8/12/13
to rubyonra...@googlegroups.com
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.

On 12 August 2013 17:38, Valentin Kotelnitski <li...@ruby-forum.com> wrote:
> 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

Valentin Kotelnitski

unread,
Aug 13, 2013, 2:43:31 AM8/13/13
to rubyonra...@googlegroups.com
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
http://guides.rubyonrails.org/migrations.html
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.

Valentin Kotelnitski

unread,
Aug 13, 2013, 2:45:57 AM8/13/13
to rubyonra...@googlegroups.com
> you are starting a new thread (as I view it in gmail
> at least) for every reply.

I just use ruby-forum interface.

Scott Ribe

unread,
Aug 13, 2013, 12:46:49 PM8/13/13
to rubyonra...@googlegroups.com
On Aug 13, 2013, at 12:43 AM, Valentin Kotelnitski <li...@ruby-forum.com> wrote:

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

Colin Law

unread,
Aug 13, 2013, 4:19:21 PM8/13/13
to rubyonra...@googlegroups.com
On 13 August 2013 17:46, Scott Ribe <scott...@elevated-dev.com> wrote:
> On Aug 13, 2013, at 12:43 AM, Valentin Kotelnitski <li...@ruby-forum.com> wrote:
>
>> 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
Reply all
Reply to author
Forward
0 new messages