2012-02-02 5 views
1

ActiveRecordで次のモデルクラスを使用しています。このクラスに相当するActiveModelを作成するにはどうすればよいですか?レールにActiveModelオブジェクトを作成する方法3

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 
+0

なぜで見つけることができますか? 「勧告」はデータベースにバックアップされていないのですか? –

+0

ちょうど一時的なオブジェクトです。私は期待されたタスクが完了するとすぐにオブジェクトを破壊しています。私はデータベースにこれらのデータを保存したくない。 – Achaius

+0

右のように、 'ActiveModel'は良い選択です。それを破壊する必要はありません –

答えて

0

私はあなたがプレーンなクラスから始め、次にActiveModelモジュールでの追加を開始することをお勧めします。さあ、検証から始めてください。

http://api.rubyonrails.org/classes/ActiveModel/Validations.html

class Recommendation 
    include ActiveModel::Validations 

    attr_accessor :from_email, :to_email, :article_id, :message 

    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 
end 

他ActiveModelドキュメントはhttp://api.rubyonrails.org/

関連する問題