【发布时间】:2012-02-02 09:15:44
【问题描述】:
我在 ActiveRecord 上有以下模型类。如何为这个类编写一个等效的 ActiveModel?
class Recommendation < ActiveRecord::Base
def self.columns() @columns ||= []; end
def self.column(name, sql_type = nil, default = nil, null = true)
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
end
column :from_email, :string
column :to_email, :string
column :article_id, :integer
column :message, :text
serialize :exception
validates_format_of :from_email, :to_email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
validates_length_of :message, :maximum => 500
belongs_to :article
end
【问题讨论】:
-
为什么?
Recommendation不是数据库支持的吗? -
它只是一个临时对象。预期任务完成后,我将立即销毁该对象。我不想将这些数据存储在数据库中。
-
对,
ActiveModel是一个不错的选择,因为这样您就不必销毁它了
标签: ruby-on-rails-3 activemodel