Forum: Ruby on Rails a newbie questionadding database field to a model basing only on schema (without model regeneration)

22ad9fbe0759f777bfbe3f963be1da1a?d=identicon&s=25 Valentin Kotelnitski (algebroid)
on 2013-08-12 17:26
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
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2013-08-12 17:40
(Received via mailing list)
On 12 August 2013 16:26, Valentin Kotelnitski <lists@ruby-forum.com>
wrote:
>       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
22ad9fbe0759f777bfbe3f963be1da1a?d=identicon&s=25 Valentin Kotelnitski (algebroid)
on 2013-08-12 17:47
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?
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2013-08-12 18:15
(Received via mailing list)
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 <lists@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
22ad9fbe0759f777bfbe3f963be1da1a?d=identicon&s=25 Valentin Kotelnitski (algebroid)
on 2013-08-12 18:38
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.
22ad9fbe0759f777bfbe3f963be1da1a?d=identicon&s=25 Valentin Kotelnitski (algebroid)
on 2013-08-12 19:57
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.
15eb13d662803f57bc4aea59704988b4?d=identicon&s=25 Scott Ribe (Guest)
on 2013-08-12 20:08
(Received via mailing list)
On Aug 12, 2013, at 11:57 AM, Valentin Kotelnitski
<lists@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_ribe@elevated-dev.com
http://www.elevated-dev.com/
(303) 722-0567 voice
A47e0a6beeb9d048ff054fc1c3a97418?d=identicon&s=25 Walter Davis (walterdavis)
on 2013-08-12 20:39
(Received via mailing list)
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
A47e0a6beeb9d048ff054fc1c3a97418?d=identicon&s=25 Walter Davis (walterdavis)
on 2013-08-12 20:40
(Received via mailing list)
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
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2013-08-12 22:14
(Received via mailing list)
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 <lists@ruby-forum.com>
wrote:
>> No you must not edit or delete an existing migration.
> 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.

>> 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
22ad9fbe0759f777bfbe3f963be1da1a?d=identicon&s=25 Valentin Kotelnitski (algebroid)
on 2013-08-13 08:43
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.
22ad9fbe0759f777bfbe3f963be1da1a?d=identicon&s=25 Valentin Kotelnitski (algebroid)
on 2013-08-13 08:45
> you are starting a new thread (as I view it in gmail
> at least) for every reply.

I just use ruby-forum interface.
15eb13d662803f57bc4aea59704988b4?d=identicon&s=25 Scott Ribe (Guest)
on 2013-08-13 18:47
(Received via mailing list)
On Aug 13, 2013, at 12:43 AM, Valentin Kotelnitski
<lists@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.

--
Scott Ribe
scott_ribe@elevated-dev.com
http://www.elevated-dev.com/
(303) 722-0567 voice
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2013-08-13 22:21
(Received via mailing list)
On 13 August 2013 17:46, Scott Ribe <scott_ribe@elevated-dev.com> wrote:
> 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
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.