新手问题 rails 的 model 该不该继承 ActiveRecord::Base

ane · 2014年03月17日 · 2354 次阅读
class User
  include Mongoid::Document
  include Mongoid::Timestamps
  include ActiveModel::ForbiddenAttributesProtection
  include ActiveModel::SecurePassword
  include Gravtastic

  gravtastic :filetype => :png, :size => 100

  field :name
  field :full_name
  field :description
  field :email
  field :password_digest
  field :password_reset_token
  field :password_reset_token_created_at, :type => Time
  field :locale, :default => I18n.locale.to_s

  has_many :spaces, :dependent => :destroy

  has_secure_password

  validates :name, :presence => true, :uniqueness => {:case_sensitive => false}, :format => {:with => /\A[a-z0-9-]+\z/, :message => I18n.t('errors.messages.space_name') }, :length => {:in => 4..20}
  validates :email, :presence => true, :uniqueness => {:case_sensitive => false}, :format => {:with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/}
  validates :password, :length => { :minimum => 6 }, :on => :create
  validates :locale, :inclusion => {:in => ALLOW_LOCALE}
  validates :current_password, :presence => true, :if => :need_current_password
  validates_length_of :password, :minimum => 6, :if => :in_password_reset

  attr_accessor :current_password, :need_current_password, :in_password_reset

  def member_spaces
    Space.where(:member_ids => self.id)
  end
end

以前写的都是直接用 rails g model 生成的,默认就继承 ActiveRecord::Base,也感觉挺好用的,最近看了一个别人的代 码,发现他的 model 是不继承 ActiveRecord,这样有什么不一样的地方?是不是必须的声明字段啊,而且貌似也能使用 validates

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请 注册新账号