Valentin Kotelnitski
2013-Aug-12 15:26 UTC
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 -- 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
Colin Law
2013-Aug-12 15:38 UTC
Re: a newbie questionadding database field to a model basing only on schema (without model regeneration)
On 12 August 2013 16:26, Valentin Kotelnitski <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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 > regenarationI 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > 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. > >-- 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLtb7sqm0K7u90skmo2GwEhR97ycEynFM5XaMjV5LxbDBw%40mail.gmail.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Valentin Kotelnitski
2013-Aug-12 15:47 UTC
Re: a newbie questionadding database field to a model basing only on schema (without model regenerat
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? -- 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/e9c3559f91b86ab31f81868eb5ab38cb%40ruby-forum.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Colin Law
2013-Aug-12 16:14 UTC
Re: Re: a newbie questionadding database field to a model basing only on schema (without model regenerat
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-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 scaffoldA 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 -- 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLtevyWiBwY0beP1Uyhm%3Dy2cWEKnKnoE4%3DGjMsb0GZrS4A%40mail.gmail.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Valentin Kotelnitski
2013-Aug-12 16:38 UTC
Re: Re: a newbie questionadding database field to a model basing only on schema (without model regen
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. > > ColinYes, 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. -- 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/12711f052d0d12ae0bb9d520ccc359ad%40ruby-forum.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Valentin Kotelnitski
2013-Aug-12 17:57 UTC
Re: a newbie questionadding database field to a model basing only on schema (without model regenerat
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. -- 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/51a70a89836c2fb6dfbd616cc2ef05a3%40ruby-forum.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Scott Ribe
2013-Aug-12 18:07 UTC
Re: a newbie questionadding database field to a model basing only on schema (without model regenerat
On Aug 12, 2013, at 11:57 AM, Valentin Kotelnitski <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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-ZCQMRMivIIdUL8GK/JU1Wg@public.gmane.org http://www.elevated-dev.com/ (303) 722-0567 voice -- 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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. For more options, visit https://groups.google.com/groups/opt_out.
Walter Lee Davis
2013-Aug-12 18:37 UTC
Re: a newbie questionadding database field to a model basing only on schema (without model regenerat
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 On Aug 12, 2013, at 2:07 PM, Scott Ribe wrote:> On Aug 12, 2013, at 11:57 AM, Valentin Kotelnitski <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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-ZCQMRMivIIdUL8GK/JU1Wg@public.gmane.org > http://www.elevated-dev.com/ > (303) 722-0567 voice > > > > > -- > 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > 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. > For more options, visit https://groups.google.com/groups/opt_out. > >-- 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/C8533C7C-9F3E-44BC-BB43-D5231413989A%40wdstudio.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Walter Lee Davis
2013-Aug-12 18:38 UTC
Re: a newbie questionadding database field to a model basing only on schema (without model regenerat
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:> On Aug 12, 2013, at 11:57 AM, Valentin Kotelnitski <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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-ZCQMRMivIIdUL8GK/JU1Wg@public.gmane.org > http://www.elevated-dev.com/ > (303) 722-0567 voice > > > > > -- > 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > 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. > For more options, visit https://groups.google.com/groups/opt_out. > >-- 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/D3C06845-7D3C-4BBD-B753-52B213990C1B%40wdstudio.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Colin Law
2013-Aug-12 20:12 UTC
Re: Re: Re: a newbie questionadding database field to a model basing only on schema (without model regen
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-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 -- 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLsSXWNo3PvO9krXUGjMcsvQMT%3DEFHA4i%2B-n-g6qdnQZkQ%40mail.gmail.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Valentin Kotelnitski
2013-Aug-13 06:43 UTC
Re: a newbie questionadding database field to a model basing only on schema (without model regenerat
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. -- 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/fdffd093f362eb61fdba2cb46e38c85e%40ruby-forum.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Valentin Kotelnitski
2013-Aug-13 06:45 UTC
Re: Re: Re: a newbie questionadding database field to a model basing only on schema (without model r
> you are starting a new thread (as I view it in gmail > at least) for every reply.I just use ruby-forum interface. -- 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/917280c577057a0591c9f8a75f8572d5%40ruby-forum.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Scott Ribe
2013-Aug-13 16:46 UTC
Re: a newbie questionadding database field to a model basing only on schema (without model regenerat
On Aug 13, 2013, at 12:43 AM, Valentin Kotelnitski <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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-ZCQMRMivIIdUL8GK/JU1Wg@public.gmane.org http://www.elevated-dev.com/ (303) 722-0567 voice -- 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/F00E8C9B-DBDB-44A2-9EFD-BE70B39E8DE5%40elevated-dev.com?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
Colin Law
2013-Aug-13 20:19 UTC
Re: a newbie questionadding database field to a model basing only on schema (without model regenerat
On 13 August 2013 17:46, Scott Ribe <scott_ribe-ZCQMRMivIIdUL8GK/JU1Wg@public.gmane.org> wrote:> On Aug 13, 2013, at 12:43 AM, Valentin Kotelnitski <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 -- 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-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLs5xWcCgs6Qph%2B4euHH3oES8iCMeTJj4d5r-nP%2BunHtUQ%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.