Active Resource? 的 find method 刚刚通过 :from 选项使搜索表达变得 更加紧凑 。这个选项撤换了之前通过向 Custom Method 添加单一的实参进行搜索的方式来限制小部分搜索结果。
比如说,之前如果您激活了一个叫做 recent 的 custom method:
Post.find(:recent) #=> GET /posts/recent.xml
现在您将需要 在第一个实参里 声明 多态 或者 范围1 (在声明的是 :all),并通过 :from 选项添加 custom method:
Post.find(:all, :from => :recent) #=> GET /posts/recent.xml
您也可以通过 :one的范围 来针对单态的资源应用同样的任务:
Post.find(:one, :from => :latest) #=> GET /post/latest.xml
并且,如果您只想亲自设定获取 Post 的地址,则可以使用实际的 URI 文字:
Post.find(:one, :from => "/categories/1/latest.xml") #=> GET /categories/1/latest.xml
如此一来,我们通过限制 find 里的第一个实参使得 资源的id 或 其范围以及任何 custom method 或 手动设置的资源地址 统统都被移动到 :from 选项中。
Active Resource? 新增的 Custom Headers
Active Resource? 通过最近的升级可以为每项资源设定 custom header 。
class Post < ActiveResource::Base headers['X-MyHeader'] = 'ryan' end
每一个来自Post的请求现在将囊括那个header。
笔记:原始的补丁所使用的 custom_header 在后来被 改为 headers 。
非常高兴看到 Active Resource? 的使用变得越来越简便!
感谢 Yudi 提供本系列文档
原文作者是 Ryan Daigle, 请访问他的博客