구글링에서 얻은 가장 만족스러운 방법은 다음이다.
Tableless models in RailsTableless models in Rails
플러그인이나 라이브러리도 필요 없고, 직접 칼럼을 선언하면 된다. 자바에서 get/set을 만드는 걸 생각하면 아무 것도 아니다.
class OnlineDemoRequest < 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 :email, :string
column :first_name, :string
column :last_name, :string
column :company, :string
validates_presence_of :email
validates_format_of :email, :with => /^$|^\S+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,4})(\]?)$/ix
validates_presence_of :first_name
validates_presence_of :last_name
validates_presence_of :company
end
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 :email, :string
column :first_name, :string
column :last_name, :string
column :company, :string
validates_presence_of :email
validates_format_of :email, :with => /^$|^\S+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,4})(\]?)$/ix
validates_presence_of :first_name
validates_presence_of :last_name
validates_presence_of :company
end
* 2.3.4에서 테스트를 했다.