Rails 3:预测2.3初学者的迁移 [英] Rails 3 : Anticipating migration for 2.3 beginners

查看:176
本文介绍了Rails 3:预测2.3初学者的迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Rails的初学者。我使用2.3.X。



我刚刚看到Rails 3已预发布[ edit:now in release candidate!



2.3我不应该采用哪些常见的编码习惯,以便交换机尽可能顺利?



编辑



我已完成家庭作业并阅读发行说明。但是对于最关键的点,它们还远远不够清楚,例如:


1.5新API



路由器和查询接口都看到了重大的,突变的变化。有一个向后兼容性层,并且将支持,直到3.1版本。


这对初学者来说不够全面像我。什么会打破?

解决方案

看看我个人的编码习惯从1.2.x开始使用Rails),下面列出了您可以根据 Rails 3发行说明



find(:all)



  Model.find(:all)
Model.find(:first)
Model.find (:last)

赞成:

  Model.all 
Model.first
Model.last

复杂查询



避免使用命名范围的复杂查询。



预期区域



Rails 3为处理ActiveRecord条件和选项提供了更简洁的方法。您可以预期它会创建自定义命名范围。

  class Model 
named_scope:limit,lambda {| value | {:limit => value}}
end

#old way
records = Model.all(:limit => 3)

#new way
records = Model.limit(3).all

#你也可以利用延迟赋值的优势
records = Model.limit(3)
#然后在你的视图
records.each {...}

升级到Rails 3时,范围定义。



常量



避免使用以下常数


  • > RAILS_ROOT 赞成Rails.root,

  • RAILS_ENV Rails.env和

  • RAILS_DEFAULT_LOGGER 以支持Rails.logger。



不引人注目的JavaScript



避免使用大量的JavaScript帮助器来支持不引人注目的JavaScript。



Gem依赖



保持 environment.rb 尽可能干净,以便更容易迁移到Bundler。您还可以预期迁移今天使用Bundler a> without Rails 3。


I am a beginner in Rails. I use 2.3.X.

I just saw Rails 3 is pre-released [edit: now in release candidate!]. I will most probably eventually switch to it.

What are the common coding habits in 2.3 I should not take, so that the switch is as smooth as possible ?

Edit:

I've done my homework and read the Release notes. But they are far from clear for the most crucial points, for example :

1.5 New APIs

Both the router and query interface have seen significant, breaking changes. There is a backwards compatibility layer that is in place and will be supported until the 3.1 release.

This is not comprehensive enough for a beginner like me. What will break ? What could I do already in 2.3.X to avoid having troubles later ?

解决方案

Looking at my personal coding habits (I have been using Rails since 1.2.x), here's a list of API changes you can anticipate according to Rails 3 release notes.

find(:all)

Avoid the usage of:

Model.find(:all)
Model.find(:first)
Model.find(:last)

in favour of:

Model.all
Model.first
Model.last

Complex queries

Avoid the composition of complex queries in favor of named scopes.

Anticipate Arel

Rails 3 offers a much cleaner approach for dealing with ActiveRecord conditions and options. You can anticipate it creating custom named scopes.

class Model
  named_scope :limit, lambda { |value| { :limit => value }}
end

# old way
records = Model.all(:limit => 3)

# new way
records = Model.limit(3).all

# you can also take advantage of lazy evaluation
records = Model.limit(3)
# then in your view
records.each { ... }

When upgrading to Rails 3, simply drop the named scope definition.

Constants

Avoid the usage of the following constants in favour of the corresponding Rails.x methods, already available in Rails 2.x.

  • RAILS_ROOT in favour of Rails.root,
  • RAILS_ENV in favour of Rails.env, and
  • RAILS_DEFAULT_LOGGER in favour of Rails.logger.

Unobtrusive Javascript

Avoid heavy JavaScript helpers in favour of unobtrusive JavaScript.

Gem dependencies

Keep your environment.rb as clean as possible in order to make easier the migration to Bundler. You can also anticipate the migration using Bundler today without Rails 3.

这篇关于Rails 3:预测2.3初学者的迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆