zoukankan      html  css  js  c++  java
  • Rails4升级小记

    升级过程中,最容易踩雷的是gem树的兼容性。所以推荐使用新分支。

    1)在老项目下使用git flow新开一个特征分支,如:

    git flow feature start rails4
    

    2)在另一个文件夹处,使用Rails4创建一个新的重名空白项目,并且使用全新gem集合,如:

    rails new myproject --database=postgresql
    

    创建gem集合,如:

    rvm use  ruby-2.0.0-p0@rails4 --create
    

    3)使用文本比较工具,将原来的老文件合并过来。

    4)将老项目下的git flow下的Rails4分支的内容清空,如下所示:

    git rm -r .
    

    将新建的Rails4项目合并过来。

    5)使用git flow 直接finish 分支合并回去。

    git flow feature finish rails4
    

    新增功能

    原生支持PostgreSQL的JSON字段

    ActiveRecord原生支持PostgreSQL的新功能,json字段格式。从migrate到调用,这条太爽了。如下所示:

    class CreateResults < ActiveRecord::Migration
      def change
        create_table :results do |t|
          t.integer :project_id
          t.json :result
    
          t.timestamps
        end
      end
    end
    

    搭配psql新出的PGStrom与Postgresql原生支持的R语言,我们继续将postgresql玩下去:D

    jbuilder已成默认配置

    每个视图目录下面,会自动生成一个.json.jbuilder文件,更好地支持json格式。

    升级踩雷

    插件兼容性

    可以参见:

    据实地测试,devise、simpleform、cancan、rolify、bettererrors、figaro等常用插件无障碍升级成功,只需要将gem地址改为:

    gem 'devise', :git => 'git://github.com/plataformatec/devise.git', :branch => "rails4"
    

    或者:

    gem 'simple_form', :git => 'git://github.com/plataformatec/simple_form.git'
    

    路由

    Rails4不再兼容同时出现两个routes的写法。需要手动改为:

      authenticated :user do
    
        root :to => 'home#index', as: :authenticated_root
       end
    
    
      root :to => "home#index" 
    

    需要将原来两个重名的路由,在其中一个后面使用as增加别名,这种写法。

    find需要大修

    不再支持findorcreatebyname等写法。会报错name表不存在的错误。如:

      #Role.find_or_create_by_name({:name => role }, :without_protection => true)
    

    需要改为:

      Role.find_or_create_by(name: role)
    

    请参考Rails4指南修改其它的,以兼容未来:

    find_all_by_... 改为: where(...).
    find_last_by_... 改为:where(...).last.
    scoped_by_... 改为: where(...).
    find_or_initialize_by_... 改为:where(...).first_or_initialize.
    find_or_create_by_... 改为:find_or_create_by(...) 或者 where(...).first_or_create.
    find_or_create_by_...! 改为 find_or_create_by!(…)或者 where(...).first_or_create!.
    

    attr_accessible默认不支持,需插件

    Rails4将attr_accessible移到控制器层面了,老写法,直接写在模型层面不再支持,如:

     attr_accessible :name, :email
    

    新写法是在控制器层面写:

     params.require(:project).permit(:name,:email)
    

    如需兼容既往,请在Gemfile中添加:

    gem 'protected_attributes'
  • 相关阅读:
    Python之推导式笔记
    利用ShardingSphere-JDBC实现分库分表--配置中心的实现
    利用ShardingSphere-JDBC实现分库分表
    MGR安装记录
    学习RadonDB源码(三)
    学习RadonDB源码(二)
    学习RadonDB源码(一)
    Spring Cloud学习笔记--Spring Boot初次搭建
    一次单核CPU占用过高问题的处理
    MySQL AutoCommit带来的问题
  • 原文地址:https://www.cnblogs.com/qinyan20/p/3666680.html
Copyright © 2011-2022 走看看