class Car
attr_accessor :wheels # this will create for you the reader and writer for this attribute
attr_accessor :doors # ya, this will do the same
# here goes all your model's stuff
end
编辑 # 2: 在阅读了 Mike 的评论之后,如果您想要 ActiveRecord 的所有功能,但是数据库中没有表的话,我会告诉您按照他的方式进行。如果您只想要一个普通的 Ruby 类,也许您会发现这个解决方案更好;)
class Model
include ActiveModel::Model
attr_accessor :var
validates :var, presence: true
end
ActiveModel::Model代码:
module ActiveModel
module Model
def self.included(base)
base.class_eval do
extend ActiveModel::Naming
extend ActiveModel::Translation
include ActiveModel::Validations
include ActiveModel::Conversion
end
end
def initialize(params={})
params.each do |attr, value|
self.public_send("#{attr}=", value)
end if params
end
def persisted?
false
end
end
end