Rails 3:2.3 初学者的预期迁移

发布于 2024-08-20 19:33:25 字数 386 浏览 6 评论 0 原文

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

我刚刚看到 Rails 3 已预发布 [编辑:现在处于候选版本中!]。我很可能最终会转向它。

2.3 中哪些常见的编码习惯是我不应该采取的,以便切换尽可能顺利?

编辑:

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

1.5 新 API

路由器和查询接口都发生了重大的突破性变化。有一个向后兼容层已经到位,并将在 3.1 版本发布之前得到支持。

对于像我这样的初学者来说,这还不够全面。什么会打破?在 2.3.X 中我可以做什么以避免以后遇到麻烦?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧梦荧光笔 2024-08-27 19:33:25

看看我个人的编码习惯(我从 1.2.x 开始就使用 Rails),这里有一个 API 更改列表,您可以根据 Rails 3 发行说明

find(:all)

避免使用:

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

赞成:

Model.all
Model.first
Model.last

复杂查询

避免复杂查询的组合,赞成命名范围。

预期 Arel

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

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

升级到 Rails 3 时,只需删除命名范围定义即可。

常量

避免使用以下常量,而使用 Rails 2.x 中已提供的相应 Rails.x 方法。

  • RAILS_ROOT 支持 Rails.root,
  • RAILS_ENV 支持 Rails.env,
  • RAILS_DEFAULT_LOGGER 支持 Rails.logger。

不显眼的 Javascript

避免使用繁重的 JavaScript 帮助程序,转而使用不显眼的 JavaScript。

Gem 依赖项

保持您的 environment.rb 尽可能干净,以便更轻松地迁移到 Bundler。您还可以立即使用 Bundler 进行迁移 没有 Rails 3。

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.

我早已燃尽 2024-08-27 19:33:25

发行说明是最需要关注的内容。除此之外,Jeremy McAnally 有一些关于整个 Rails 3 事物的简洁的博客文章(并且刚刚发布了gem 来帮助您进行迁移)。

The release notes are the most important thing to keep an eye on. Other than that Jeremy McAnally has some neat blog posts about the whole Rails 3 thing (and has just released a gem to help you with the migration).

初见你 2024-08-27 19:33:25

我想说,请阅读 rails 发行说明,亲自看看什么似乎更令人惊讶你。很多东西都发生了变化,所以阅读本文绝对非常重要。

I'd say, read the rails release notes and see for yourself what seems the more surprising to you. A lot of stuff changed so reading this is definitively very important.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文