I have been searching for articles and code relating to setting default values inside the model and I have found several different approaches to this issue. I am therefore inquiring if the community has formed any consensus on the "best practice" in this regard? There are frequent occasions when a sensible default value is determinable from the context and to save redundant user input I would like to provide these values automatically. Further, I happen to subscribe to the not null school of database design, the only (rare) exception being the case of a presently unknown but applicable value. This means that that these fields require some input or the DBMS constraints are going trip. One interesting item that I ran across is the active_record_defaults plugin by a fellow from NZ. Does anyone have any comments on this plugin or can offer any alternatives? Regards, -- 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 -~----------~----~----~----~------~----~------~--~---
You can do it by passing default parameters to the model on new. You can push this down from the controller to the model as a method if you want, e.g. @user=User.new_with_defaults The view will then pick up the defaults. or if you want to make sure that the defaults are in place in all places where a new object might be used then stick the code in ''after_initialize''. If you need to do some calcs then there is always ''before_validation_on_create'' or just ''before_create''. If you need to do the calcs all the time then ''before_save''. It depends what you''re trying to achieve. What you don''t do is rely upon defaults in the database. For me the default calculations have to be in the model. On Mar 7, 4:13 pm, James Byrne <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I have been searching for articles and code relating to setting default > values inside the model and I have found several different approaches to > this issue. I am therefore inquiring if the community has formed any > consensus on the "best practice" in this regard?--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Neil Wilson wrote:> > It depends what you''re trying to achieve.For now, I wish to provide a simple, explicit, and model-centric means of specifying default values for certain table attributes. Since I posted above I have installed the active_record_defaults plugin and this seems to provide most of what I desired. For example: Class MyTable < ActiveRecord::Base belongs_to :my_other_table # code dependent on active_record_defaults plugin default :effective_from => Time.now defaults :my_attribute => "unknown", :my_int_attribute => 21 validates_presence_of :another_attribute> > What you don''t do is rely upon defaults in the database. For me the > default calculations have to be in the model. >My practice is that default values are mostly, if not entirely, localized to the model or constrained within the DB schema. I depart from Rails orthodoxy on this issue and I selected my database backend (postgresql) with care so that significant amount of data integrity logic could be enforced by the database. For development I want this sort of stuff close to the code so that I can easily adjust things without having to alter the DB overmuch. In production I anticipate that a good deal of the constraints and defaults will be moved from the model into the DBMS. Calculated fields are a distinct issue, as you point out, and on that matter the model indeed may be the best (perhaps only) place for them. Thanks, -- 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 -~----------~----~----~----~------~----~------~--~---