http://github.com/parasew/acts_as_tree
rails s
rake db:migrate
rails g scaffold Category name:string memo:text
rails g migrate add_categories_uuid_colums
bundle install
https://github.com/liuqiang/webschool40/tree/master/vendor/
http://www.douban.com/group/topic/8537205/
Named Scopes
下面我们看看在Rails 3中named scopes的变化。假如,在Article下有两个named scopes,visible用来表示隐藏的文章,published用来表示已经发布的文章。
- #/app/models/article.rb
- class Article < ActiveRecord::Base
- named_scope :visible, :conditions => ["hidden != ?", true]
- named_scope :published, lambda { {:conditions => ["published_at <= ?", Time.zone.now]} }
- end
上面的语句是Rails 2中定义named scoped的方式,在Rails 3中named scoped的语法稍微有点不同。首先是用scoped代替named scoped,其次,和find的变化相同,不再使用find的哈希参数,而是改用同名的方法,而conditions会变成where如下:
- #/app/models/article.rb
- class Article < ActiveRecord::Base
- scope :visible, where("hidden != ?", true)
- scope :published, lambda { where("published_at <= ?", Time.zone.now) }
- end
另外一个关于scope的新功能是可以通过scope创建scope,例如,我们希望创建一个叫做recent的scope,表示发布过并且没有 隐藏的文章列表,并且文章列表按照发布的降序排列。那么,我们就可以通过使用visible和pulished来实现如下:
- scope :recent, visible.published.order("published_at desc")
如上所示,我们就是通过将两个scopes连起来,并且添加一个排序的方法来创建scope的。而且,我们也可以通过在控制台调试我们的 scope,发现当我们调用Article.recent时,会返回一个ActiveRecord::NamedScope::Scope的对象,而这个 对象会和ActiveRecord::Relation有相似的功能,即lazy loading不是当即查询。如下:
- ruby-1.9.1-p378 > Article.recent
- => #<ActiveRecord::NamedScope::Scope:0x0000010318bd08 @table=#<Arel::Table:0x00000102740ea8
- @name="articles", @options={:engine=>#<Arel::Sql::Engine:0x00000102651900 @ar=ActiveRecord::Base>},
- @engine=#<Arel::Sql::Engine:0x00000102651900 @ar=ActiveRecord::Base>>, …
当我们查询所有时,我们将会看到对应的Article对象返回。(译者晓夜注:就是说在用的时候返回)
- ruby-1.9.1-p378 > Article.recent.all
- => [#<Article id: 1, name: "It's Ancient", published_at: "2010-01-01",
- hidden: false, created_at: "2010-02-22 20:35:42", updated_at: "2010-02-22 23:00:16">]
http://hi.baidu.com/hbxiao135/blog/item/58f773da137c93d7b6fd4893.html