• 去看一下 becomes 和 becomes!方法

    class AAddress < Address
    
    end
    
    class BAddress < Address
    
    end
    
    a_address = Address.new(type: 'AAddress')
    puts a_address.class.name  
    结果 'AAddress'
    
    puts a_address.becomes(Address).class.name
    结果 'Address'
    
  • XxxJob.set(wait_until: xx).perform_later(xx) 收到消息以后,把这个队列任务删掉,然后和上面一样再重新创建一条定时任务

  • 你说数据量不大的话,可以试试这样,我目前这样导觉得还挺方便的。

    your_app/scripts//migrate/database.yml

    to:
      adapter: mysql2
      pool: 5
      reconnect: true
      username: x
      password: x
      host: x
      database: x
    from:
      adapter: mysql2
      pool: 5
      reconnect: true
      username: x
      password: x
      host: x
      database: x
    

    your_app/scripts//migrate/migrate_db.rb

    require 'active_record'
    require 'yaml'
    ROOT = File.expand_path("..",__FILE__)
    DB_CONFIG = YAML.load(File.new("#{ROOT}/database.yml"))
    class FromDB < ActiveRecord::Base
      self.record_timestamps = false
      self.abstract_class = true
      establish_connection(DB_CONFIG['from'])
    end
    class ToDB < ActiveRecord::Base
      self.record_timestamps = false
      self.abstract_class = true
      establish_connection(DB_CONFIG['to'])
    
      def self.save_from(obj)
        n = self.find_by_id(obj.id) || self.new
        self.column_names.each do |c|
          m = "convert_#{c}"
          if obj.respond_to?(m)
            n.send("#{c}=", obj.send(m))
          else
            n.send("#{c}=", obj.send(c)) if obj.respond_to?(c)
          end
        end
        n.save
        n
      end
    end
    

    copy上面的代码

    eg: 迁移direct_ship_prices的数据

    #字段名和属性不一样的,写一个convert_new_column方法转换,一样的就不用写。

    your_app/scripts//migrate/models/direct_ship_price.rb

    require File.expand_path("../../migrate_db.rb",__FILE__)
    
    class FromDirectShipPrice < FromDB
      self.table_name = 'direct_ship_prices'
      #以前的公司id叫client_id,现在叫company_id
      def convert_company_id
        self.client_id
      end
      #以前的status是integer,现在是boolean
      def convert_status
            self.status == 0 ? false : true
      end
    
    end
    
    
    class ToDirectShipPrice < ToDB
      self.table_name = 'direct_ship_prices'
    end
    
    FromDirectShipPrice.find_each do |src|
      ToDirectShipPrice.save_from(src)
    end
    

    最后 rails runner -e ENV script/migrate/models/direct_ship_price.rb

  • mina setup 出现一些问题。 at 2016年08月31日

    好了,非常感谢😆 。我去了解一下rbenv。