class Article < ActiveRecord::Base
validates :title, :presence => true
validates :body, :presence => true
belongs_to :user
has_and_belongs_to_many :categories
has_many :comments
scope :published, where("articles.published_at IS NOT NULL")
scope :draft, where("articles.published_at IS NULL")
def long_title
"#{title} - #{published_at}"
end
end
Named Scope
本文探讨了 ActiveRecord 模型中验证规则的应用及多种关联关系的定义方法。包括必填验证、模型之间的属于关系、多对多关系及一对多关系等。此外,还介绍了如何使用 scope 方法来定义模型的查询范围。

被折叠的 条评论
为什么被折叠?



