I''ve got a form text field (using a dynarch calendar helper) that''s for a date value. If the user submits the form without a date entered, I want that date to be set to 15 days from today. I''ve been putzing around with overriding the update_attributes method, but I can''t seem to nail the correct format. I know this is probably simple, but I seem to be making it hard. Thanks -- Marlon "And I Sleep, and I dream of the person I might have been, and I''ll be free again And I Speak, like someone who''s been to the highest peaks, and back again And I Swear, that my grass is greener than anyones, until I believe again"
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Sep 28, 2005, at 8:33 PM, Marlon Moyer wrote:> I''ve got a form text field (using a dynarch calendar helper) that''s > for a date value. If the user submits the form without a date > entered, I want that date to be set to 15 days from today. I''ve been > putzing around with overriding the update_attributes method, but I > can''t seem to nail the correct format. I know this is probably > simple, but I seem to be making it hard.How about: class Foo < ActiveRecord::Base before_save { |foo| foo.date ||= 15.days.from_now } end jeremy -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (Darwin) iD8DBQFDO4ZKAQHALep9HFYRAr5eAKDjUWUOchmMUeJ4jP2YopXZJNK4dgCfdkum zYrTyCUCbEewVND0StqO09Y=HP1E -----END PGP SIGNATURE-----
On Thursday 29 September 2005 05:33, Marlon Moyer wrote:> I''ve got a form text field (using a dynarch calendar helper) that''s > for a date value. If the user submits the form without a date > entered, I want that date to be set to 15 days from today. I''ve been > putzing around with overriding the update_attributes method, but I > can''t seem to nail the correct format. I know this is probably > simple, but I seem to be making it hard.class MyClass def instantiate(*args) super(*args) self.theDate ||= 15.days.from.now end end Michael -- Michael Schuerig Airtight arguments have mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org vacuous conclusions. http://www.schuerig.de/michael/ --A.O. Rorty, Explaining Emotions
Thanks Jeremy and Michael....I knew it was a lot more simple than I was making it out to be. On 9/29/05, Michael Schuerig <michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org> wrote:> On Thursday 29 September 2005 05:33, Marlon Moyer wrote: > > I''ve got a form text field (using a dynarch calendar helper) that''s > > for a date value. If the user submits the form without a date > > entered, I want that date to be set to 15 days from today. I''ve been > > putzing around with overriding the update_attributes method, but I > > can''t seem to nail the correct format. I know this is probably > > simple, but I seem to be making it hard. > > class MyClass > def instantiate(*args) > super(*args) > self.theDate ||= 15.days.from.now > end > end > > Michael > > -- > Michael Schuerig Airtight arguments have > mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org vacuous conclusions. > http://www.schuerig.de/michael/ --A.O. Rorty, Explaining Emotions > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Marlon "And I Sleep, and I dream of the person I might have been, and I''ll be free again And I Speak, like someone who''s been to the highest peaks, and back again And I Swear, that my grass is greener than anyones, until I believe again"
Its questions like this that I really like this list. I don''t know if its just that I don''t know Ruby that well of something else, because I wouldn''t have thought of this answer. I think my brain is to ingrained to think in the patterns of other programming languages to come up with such an elegant and simplistic answer. I have lost track of the number of times I have tried to do things the hard way when I am building my Rails app. - Michael On 9/29/05, Marlon Moyer <marlon.moyer-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks Jeremy and Michael....I knew it was a lot more simple than I > was making it out to be. > > > On 9/29/05, Michael Schuerig <michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org> wrote: > > On Thursday 29 September 2005 05:33, Marlon Moyer wrote: > > > I''ve got a form text field (using a dynarch calendar helper) that''s > > > for a date value. If the user submits the form without a date > > > entered, I want that date to be set to 15 days from today. I''ve been > > > putzing around with overriding the update_attributes method, but I > > > can''t seem to nail the correct format. I know this is probably > > > simple, but I seem to be making it hard. > > > > class MyClass > > def instantiate(*args) > > super(*args) > > self.theDate ||= 15.days.from.now > > end > > end > > > > Michael > > > > -- > > Michael Schuerig Airtight arguments have > > mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org vacuous conclusions. > > http://www.schuerig.de/michael/ --A.O. Rorty, Explaining Emotions > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > -- > Marlon > > "And I Sleep, and I dream of the person I might have been, and I''ll be > free again > And I Speak, like someone who''s been to the highest peaks, and back again > And I Swear, that my grass is greener than anyones, until I believe again" > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On 29-sep-2005, at 11:31, Michael Schuerig wrote:> On Thursday 29 September 2005 05:33, Marlon Moyer wrote: > >> I''ve got a form text field (using a dynarch calendar helper) that''s >> for a date value. If the user submits the form without a date >> entered, I want that date to be set to 15 days from today. I''ve been >> putzing around with overriding the update_attributes method, but I >> can''t seem to nail the correct format. I know this is probably >> simple, but I seem to be making it hard. >> > > class MyClass > def instantiate(*args) > super(*args) > self.theDate ||= 15.days.from.now > end > endNow this is some serious ruby foo. This is also very neat for testing, when you know you should be showing some sort of "upcoming items" on a page. For this in my fixtures I was putting deadline: <%= (Time.now + 15.days).to_s %> but now it will be deadline: <%= 15.days.from.now.to_s %> This language never seizes to amaze me (however I started to percieve ActiveSupport like stdlib which it is not). -- Julian "Julik" Tarkhanov
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Oct 2, 2005, at 12:18 PM, Julian ''Julik'' Tarkhanov wrote:> Now this is some serious ruby foo. This is also very neat for > testing, when you know you should be showing > some sort of "upcoming items" on a page. For this in my fixtures I > was putting > > deadline: <%= (Time.now + 15.days).to_s %> > > but now it will be > > deadline: <%= 15.days.from.now.to_s %>The method name is from_now and the to_s is implicit, so: deadline: <%= 15.days.from_now %> Regards, jeremy -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (Darwin) iD8DBQFDQGh1AQHALep9HFYRAiVCAKDOKlvgLcjHIbyouuA29Jpb+/uVVwCeOuOW 5tAiowds4emmDUuhE4cAas0=+ym9 -----END PGP SIGNATURE-----