新手问题 model 里的属性

suyuhang · April 25, 2014 · 1807 hits

最近我一直苦恼一个问题,当我建立了一个 model 的时候,写了一个 api

然后我通过定义类

class Comic < ActiveRecord::Base

attr_accessible :name

def name self.name 或者写成 attributes['name'] end

def as_json(options={}) super(:methods => [:name]) end

end

我有一个疑问,self 是当前的 class,self.name 和 attributes['name'] 的区别,

我知道后者这么访问属性是一个 hash

但是我比较纠结的是 每一个类 都是一个 hash 嚒?

而且我在 method name 中对 name 属性 进行 URI:escape() 时候,却发现,URI:escape(self.name) 报错,而 URI:escape(arrtibutes['name']) 却可以顺利进行。

我是个新手 我想问问 这块应该仔细的看哪里。

No Reply at the moment.
You need to Sign in before reply, if you don't have an account, please Sign up first.