Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 类型安全轨道3无表模型_Ruby On Rails_Ruby_Ruby On Rails 3_Activemodel - Fatal编程技术网

Ruby on rails 类型安全轨道3无表模型

Ruby on rails 类型安全轨道3无表模型,ruby-on-rails,ruby,ruby-on-rails-3,activemodel,Ruby On Rails,Ruby,Ruby On Rails 3,Activemodel,这描述了如何在Rails 3中设置无表模型,如中所示: class Message include ActiveModel::Validations include ActiveModel::Conversion extend ActiveModel::Naming attr_accessor :name, :email, :content validates_presence_of :name validates_format_of :email, :with =&

这描述了如何在Rails 3中设置无表模型,如中所示:

class Message
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  attr_accessor :name, :email, :content

  validates_presence_of :name
  validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
  validates_length_of :content, :maximum => 500

  def initialize(attributes = {})
    attributes.each do |name, value|
      send("#{name}=", value)
    end
  end

  def persisted?
    false
  end
end
它工作得很好,但它没有让Rails知道属性的类型。这意味着,当各种插件/库工作时,它们倾向于退回到有效地将属性视为“任意”类型。例如,to_xml将它们列为类型“yaml”

有没有办法告诉Rails无表模型中的属性类型?

您应该看看,它提供了如下类型的属性:

class Person
  include ActiveAttr::TypecastedAttributes
  attribute :age, :type => Integer
end
还有一个。

还有宝石。它是创建无表ActiveRecord模型的一颗宝石,因此它支持验证、关联和类型


您在问题中描述的方式不支持关联或(正如您所发现的)类型。

我想您可以在中找到一些答案。和你的问题相似。