I have a database column named ''status'' in my ''orders'' table. When an order placed, I''d like to initialize this column value to be ''in progress''. Is it best to do this in: A) the database migration B) the database itself C) in before_create My personal preference is to do it in the before_create. This way it is very evident to me when I look at the model as to what the value is going to be initialized to. Are there performance issues with doing this? Jody Baty Lead Developer my Curriculum Analysis Tools (www.mycatshq.com) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Jody, i am using Oracle for back-end storage, so all my default values for columns are in sql scripts, which is separate folder, and i don''t think introduction of default value in any of the 3 places you mentioned will lead to performance problem. (Worst case: if it affects also it should be very very negligible, but i don''t have benchmark results, personally i feel, it will not) On Sep 12, 1:23 am, JodyB <j...-+M9Zu1f3Liz9Z3uNjs/D0KnAVvkbxe8d@public.gmane.org> wrote:> I have a database column named ''status'' in my ''orders'' table. When an > order placed, I''d like to initialize this column value to be ''in > progress''. Is it best to do this in: > > A) the database migration > B) the database itself > C) in before_create > > My personal preference is to do it in the before_create. This way it > is very evident to me when I look at the model as to what the value is > going to be initialized to. Are there performance issues with doing > this? > > Jody Baty > Lead Developer > my Curriculum Analysis Tools (www.mycatshq.com)--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
JodyB wrote:> I have a database column named ''status'' in my ''orders'' table. When an > order placed, I''d like to initialize this column value to be ''in > progress''. Is it best to do this in: > > A) the database migration > B) the database itself > C) in before_create > > My personal preference is to do it in the before_create. This way it > is very evident to me when I look at the model as to what the value is > going to be initialized to. Are there performance issues with doing > this?I don''t think there are performance issues to worry about. The before_create callback imposes a bit of overhead, but it''s nothing to worry about. I''d go with the before_create approach, since I like to keep as much of my biz logic in Ruby as I can. Makes it easier to tell what''s going on, it''s more portable, and I''m better at Ruby than SQL. Note that if you are using before_create callback and you make an object with #new instead of #create, you can have an object floating around that hasn''t had that attribute initialized as you might expect. For those situtations, I often do lazy initialization on accessors: def foo self.attributes[:foo] ||= "default value" end -- Josh Susser http://blog.hasmanythrough.com -- 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 post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I''m using DEFAULT attribute (set to ''pending'') on a column inside mysql. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 9/11/07, JodyB <jody-+M9Zu1f3Liz9Z3uNjs/D0KnAVvkbxe8d@public.gmane.org> wrote:> > I have a database column named ''status'' in my ''orders'' table. When an > order placed, I''d like to initialize this column value to be ''in > progress''. Is it best to do this in: > > A) the database migration > B) the database itself > C) in before_create > > My personal preference is to do it in the before_create. This way it > is very evident to me when I look at the model as to what the value is > going to be initialized to. Are there performance issues with doing > this?To give you yet another option, there''s a dead simple little plugin called default_values here: http://svn.rubaidh.com/plugins/trunk/default_values You then just declare default values in your model: class Order < ActiveRecord::Base default_values :status => ''in progress'' end --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks, I''m certainly going to give this plugin a try. It''s exactly what I''m looking for: expressive and easy to implement. Jody On Sep 12, 1:13 pm, "Bob Showalter" <showa...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 9/11/07, JodyB <j...-+M9Zu1f3Liz9Z3uNjs/D0KnAVvkbxe8d@public.gmane.org> wrote: > > > > > I have a database column named ''status'' in my ''orders'' table. When an > > order placed, I''d like to initialize this column value to be ''in > > progress''. Is it best to do this in: > > > A) the database migration > > B) the database itself > > C) in before_create > > > My personal preference is to do it in the before_create. This way it > > is very evident to me when I look at the model as to what the value is > > going to be initialized to. Are there performance issues with doing > > this? > > To give you yet another option, there''s a dead simple little plugin > called default_values here: > > http://svn.rubaidh.com/plugins/trunk/default_values > > You then just declare default values in your model: > > class Order < ActiveRecord::Base > default_values :status => ''in progress'' > end--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---