A More User-friendly date_select() Alternative in Rails

ryan / Tue, 27 Jun 2006 05:56:00 GMT

5 comments

This article has moved over to our sister spin-off blog, RubyJudo, which focuses on more arcane technical topics than NotRocketSurgery.

Comments / Leave a response

  • xain said 164 days later:

    I don’t know how to read date_select ’s return. I use @user.update_attribute(:birthday, params[:user][:birthday]) It reports a error.

  • JD said 165 days later:

    I can understand why this would be confusing. The date data is parsed by ActiveRecord, so you generally need to do:

    @user.update_attributes(params[:user])
    

    But, if you want more control, things get pretty kludgy. You can do this:

    user = User.new(params[:user])
    @user.update_attribute(:birthday, user.birthday)
    

    There might be a better way, but I don’t know of one.

  • walflour said 325 days later:

    using rails 1.2.x the @attribute trick doesn’t seem to work any longer. And I think that the better way to do this is to overload the attribute setter like so:

    @class User < ActiveRecord::Base end@

    def birthdate=(new_birthdate)
    end
    [do magic on string new_birthdate]
    [and store in tmp_birthdate]
    write_attribute("birthdate", tmp_birthdate)
  • walflour said 325 days later:

    that got mangled

    
    class User < ActiveRecord::Base
      def birthdate=(new_birthdate)
        [do magic on string new_birthdate]
        [and store in tmp_birthdate]
    
        write_attribute("birthdate", tmp_birthdate)
      end
    end
    
    

(leave url/email »)

   Comment Markup Help