ROR doesn''t realize functionality of setting default values for models. We can use db''s default values but they are evaluated during migrations. We can''t set default value i.e. Time.now. But there are completed gems that realize this. I.e. default_value_for (https://github.com/FooBarWidget/default_value_for). Why not to merge it in rails? Thanks. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/u1PNgYmkABUJ. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Isn''t this just the same as having a before_save callback where you define these default values? Also, what do you mean with "We can use db''s default values but they are evaluated during migrations"? On Oct 17, 2012, at 8:28 PM, Alexander Kurakin wrote:> ROR doesn''t realize functionality of setting default values for models. We can use db''s default values but they are evaluated during migrations. We can''t set default value i.e. Time.now. But there are completed gems that realize this. I.e. default_value_for (https://github.com/FooBarWidget/default_value_for). Why not to merge it in rails? Thanks. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. > To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/u1PNgYmkABUJ. > To post to this group, send email to rubyonrails-core@googlegroups.com. > To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.Cumprimentos, Luís Ferreira -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
You can read here: https://github.com/FooBarWidget/default_value_for#when-not-to-use-default_value_for среда, 17 октября 2012 г., 23:41:55 UTC+4 пользователь Zamith написал:> > Isn''t this just the same as having a before_save callback where you define > these default values? > > Also, what do you mean with "We can use db''s default values but they are > evaluated during migrations"? > On Oct 17, 2012, at 8:28 PM, Alexander Kurakin wrote: > > ROR doesn''t realize functionality of setting default values for models. We > can use db''s default values but they are evaluated during migrations. We > can''t set default value i.e. Time.now. But there are completed gems that > realize this. I.e. default_value_for ( > https://github.com/FooBarWidget/default_value_for). Why not to merge it > in rails? Thanks. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Core" group. > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-core/-/u1PNgYmkABUJ. > To post to this group, send email to rubyonra...@googlegroups.com<javascript:> > . > To unsubscribe from this group, send email to > rubyonrails-co...@googlegroups.com <javascript:>. > For more options, visit this group at > http://groups.google.com/group/rubyonrails-core?hl=en. > > > Cumprimentos, > Luís Ferreira > > > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/jzPxLjH_J-YJ. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
That is a problem for me either. I can''t use before_save callback because I need to show the default value on forms. So, I have a date field and the default value is today, but when user open the form, today must be on that field. I''m setting this kind of default value on controller like that: def new @payment = Payment.new @payment.date = Date.current end But this become a mess since I have "dynamic" default values in almost every model of my application. On Wednesday, October 17, 2012 4:41:55 PM UTC-3, Zamith wrote:> > Isn''t this just the same as having a before_save callback where you define > these default values? > > Also, what do you mean with "We can use db''s default values but they are > evaluated during migrations"? > On Oct 17, 2012, at 8:28 PM, Alexander Kurakin wrote: > > ROR doesn''t realize functionality of setting default values for models. We > can use db''s default values but they are evaluated during migrations. We > can''t set default value i.e. Time.now. But there are completed gems that > realize this. I.e. default_value_for ( > https://github.com/FooBarWidget/default_value_for). Why not to merge it > in rails? Thanks. > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Core" group. > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-core/-/u1PNgYmkABUJ. > To post to this group, send email to rubyonra...@googlegroups.com<javascript:> > . > To unsubscribe from this group, send email to > rubyonrails-co...@googlegroups.com <javascript:>. > For more options, visit this group at > http://groups.google.com/group/rubyonrails-core?hl=en. > > > Cumprimentos, > Luís Ferreira > > > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/Q5Pjbb3Oow0J. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
This is relevant: http://stackoverflow.com/questions/328525/what-is-the-best-way-to-set-default-values-in-activerecord They mention using after_initialize. Allen Madsen http://www.allenmadsen.com On Thu, Oct 18, 2012 at 8:50 AM, Gabriel Sobrinho < gabriel.sobrinho@gmail.com> wrote:> That is a problem for me either. > > I can''t use before_save callback because I need to show the default value > on forms. > > So, I have a date field and the default value is today, but when user open > the form, today must be on that field. > > > I''m setting this kind of default value on controller like that: > > def new > @payment = Payment.new > @payment.date = Date.current > end > > But this become a mess since I have "dynamic" default values in almost > every model of my application. > > > On Wednesday, October 17, 2012 4:41:55 PM UTC-3, Zamith wrote: > >> Isn''t this just the same as having a before_save callback where you >> define these default values? >> >> Also, what do you mean with "We can use db''s default values but they are >> evaluated during migrations"? >> On Oct 17, 2012, at 8:28 PM, Alexander Kurakin wrote: >> >> ROR doesn''t realize functionality of setting default values for models. >> We can use db''s default values but they are evaluated during migrations. We >> can''t set default value i.e. Time.now. But there are completed gems that >> realize this. I.e. default_value_for (https://github.com/** >> FooBarWidget/default_value_for<https://github.com/FooBarWidget/default_value_for> >> **). Why not to merge it in rails? Thanks. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Ruby on Rails: Core" group. >> To view this discussion on the web visit https://groups.google.com/d/** >> msg/rubyonrails-core/-/**u1PNgYmkABUJ<https://groups.google.com/d/msg/rubyonrails-core/-/u1PNgYmkABUJ> >> . >> To post to this group, send email to rubyonra...@googlegroups.**com. >> To unsubscribe from this group, send email to rubyonrails-co...@** >> googlegroups.com. >> >> For more options, visit this group at http://groups.google.com/** >> group/rubyonrails-core?hl=en<http://groups.google.com/group/rubyonrails-core?hl=en> >> . >> >> >> Cumprimentos, >> Luís Ferreira >> >> >> >> -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Core" group. > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-core/-/Q5Pjbb3Oow0J. > > To post to this group, send email to rubyonrails-core@googlegroups.com. > To unsubscribe from this group, send email to > rubyonrails-core+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/rubyonrails-core?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Using a after initialize does not respect if you set the attribute to nil, like this: Payment.new(:due_date => nil) Also, it will mutate the already persisted objects if they are persisted using a nil due date for example. Another problem happens when you select specific attributes like: Payment.select(:id, :person_id).first It raises an exception on after initialize hook because there is no due date attribute :( On Thu, Oct 18, 2012 at 12:00 PM, Allen Madsen <allen.c.madsen@gmail.com> wrote:> This is relevant: > http://stackoverflow.com/questions/328525/what-is-the-best-way-to-set-default-values-in-activerecord > > They mention using after_initialize. > > Allen Madsen > http://www.allenmadsen.com > > > > On Thu, Oct 18, 2012 at 8:50 AM, Gabriel Sobrinho > <gabriel.sobrinho@gmail.com> wrote: >> >> That is a problem for me either. >> >> I can''t use before_save callback because I need to show the default value >> on forms. >> >> So, I have a date field and the default value is today, but when user open >> the form, today must be on that field. >> >> >> I''m setting this kind of default value on controller like that: >> >> def new >> @payment = Payment.new >> @payment.date = Date.current >> end >> >> But this become a mess since I have "dynamic" default values in almost >> every model of my application. >> >> >> On Wednesday, October 17, 2012 4:41:55 PM UTC-3, Zamith wrote: >>> >>> Isn''t this just the same as having a before_save callback where you >>> define these default values? >>> >>> Also, what do you mean with "We can use db''s default values but they are >>> evaluated during migrations"? >>> On Oct 17, 2012, at 8:28 PM, Alexander Kurakin wrote: >>> >>> ROR doesn''t realize functionality of setting default values for models. >>> We can use db''s default values but they are evaluated during migrations. We >>> can''t set default value i.e. Time.now. But there are completed gems that >>> realize this. I.e. default_value_for >>> (https://github.com/FooBarWidget/default_value_for). Why not to merge it in >>> rails? Thanks. >>> >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "Ruby on Rails: Core" group. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msg/rubyonrails-core/-/u1PNgYmkABUJ. >>> To post to this group, send email to rubyonra...@googlegroups.com. >>> To unsubscribe from this group, send email to >>> rubyonrails-co...@googlegroups.com. >>> >>> For more options, visit this group at >>> http://groups.google.com/group/rubyonrails-core?hl=en. >>> >>> >>> Cumprimentos, >>> Luís Ferreira >>> >>> >>> >> -- >> You received this message because you are subscribed to the Google Groups >> "Ruby on Rails: Core" group. >> To view this discussion on the web visit >> https://groups.google.com/d/msg/rubyonrails-core/-/Q5Pjbb3Oow0J. >> >> To post to this group, send email to rubyonrails-core@googlegroups.com. >> To unsubscribe from this group, send email to >> rubyonrails-core+unsubscribe@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/rubyonrails-core?hl=en. > > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Core" group. > To post to this group, send email to rubyonrails-core@googlegroups.com. > To unsubscribe from this group, send email to > rubyonrails-core+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/rubyonrails-core?hl=en.-- Cheers, Gabriel Sobrinho -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
On Oct 18, 2012, at 8:50 AM, Gabriel Sobrinho wrote:> That is a problem for me either. > > I can''t use before_save callback because I need to show the default value on forms. > > So, I have a date field and the default value is today, but when user open the form, today must be on that field. > > > I''m setting this kind of default value on controller like that: > > def new > @payment = Payment.new > @payment.date = Date.current > endOne method I was surprised to not see on Stack Overflow - overriding the accessor: class Payment < AR::Base def date super || write_attribute(:date, Date.current) end end This still has the issue identified by G. Sobrinho, however, where explicitly setting an attribute to nil doesn''t work quite right. --Matt Jones -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.