诗歌rails 之Tableless Model
# models/recommendation.rb
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
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
# recommendations_controller.rb
def new
@recommendation = Recommendation.new(:article_id => params[:article_id])
end
def create
@recommendation = Recommendation.new(params[:recommendation])
if @recommendation.valid?
# send email
flash[:notice] = "Successfully created recommendation."
redirect_to root_url
else
render :action => 'new'
end
end
莫愁前路无知己,天下无人不识君。