Rails Gem does not set the setter method

I tried to put up a Ruby Gem based on Railscast Episode 033 Making a Plugin. I got the gem created and uploaded to Rubygems.org as stringify-time, but the setter method is not working.

The GitHub Repo

  • Rails Migration - PG::Error: ERROR: invalid input syntax for integer: “”
  • Multiple sum()s in ActiveRecord in Rails
  • how to handle ActiveRecord::RecordNotFound in rails controller?
  • How do I get Rails to eager load counts?
  • Updating extra attributes in a has_many, :through relationship using Rails
  • Rails Model Validation Conditional allow_nil?
  • The main part of the gem

    module StringifyTime
      def stringify_time(*names)
        names.each do |name|
          define_method "#{name}_string" do
            read_attribute(name).to_s(:db) unless read_attribute(name).nil?
          define_method "#{name}_string=" do |time_str|
              write_attribute("#{name}", Time.zone.parse(time_str))
            rescue ArgumentError
              instance_variable_set("@#{name}_invalid", true)
          define_method "#{name}_invalid?" do
    ActiveRecord::Base.send(:extend, StringifyTime)

    A attribute symbol is passed in and getter and setter methods are created. The getter method works, but when I try to update the attribute through the form, the data is not saved.

    using the stringify_time method in the Task model

    class Task < ActiveRecord::Base                                            
      validates_presence_of :name                                              
      stringify_time :due_at                                                   
      # def due_at_string                                                      
      #   due_at.to_s(:db)                                                     
      # end                                                                    
      # def due_at_string=(due_at_str)                                         
      #   self.due_at = Time.parse(due_at_str)                                 
      # rescue ArgumentError                                                   
      #   @due_at_invalid = true                                               
      # end                                                                    
      def validate                                                             
        errors.add :due_at, 'is invalid' if due_at_invalid? # @due_at_invalid  

    When I uncomment the setter method I wrote manually, then I can edit the attribute.

  • Gem environment for running rb-libsvm
  • Load Ruby on Rails models without loading the entire framework
  • Setting classpath for org.jruby.Main?
  • Geokit query not returning expected data fields in Rails 3
  • How to find out how many characters came before image tag using Nokogiri?
  • Compute equidistant GPS point around a center
  • One Solution collect form web for “Rails Gem does not set the setter method”

    Right after I tried to use the generated method in the rails console like @matjis-van-zuijlen suggested in the comments, I was able to update the due_at attribute. The issue was me not whitelisting :due_at_string in the controller.

    def task_params                                                                                                                                       
      params.require(:task).permit(:name, :due_at, :due_at_string)                                                                                        
    Ruby is the best programming language in the world - Ruby on Rails.