ruby-on-rails – ActiveModel属性

如何获取ActiveRecord属性方法功能我有这堂课:
class PurchaseForm
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  attr_accessor :name,:surname,:email

  validates_presence_of :name

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

  def initialize(attributes = {},shop_name)
    if not attributes.nil?
      attributes.each do |name,value|
      send("#{name}=",value)
    end
  end

  def persisted?
    false
  end
end

我需要做什么,有一个属性方法列出来自PurchaseForm对象的所有名称和值?

解决方法

这是重构的变体:
class PurchaseForm
  include ActiveModel::Model

  def self.attributes
    [:name,:email]
  end

  attr_accessor *self.attributes

  # your validations

  def to_hash
    self.class.attributes.inject({}) do |hash,key|
      hash.merge({ key => self.send(key) })
    end
  end  
end

现在您可以轻松使用此类:

irb(main):001:0> a = PurchaseForm.new({ name: 'Name' })
=> #<PurchaseForm:0x00000002606b50 @name="Name">
irb(main):002:0> a.to_hash
=> {:name=>"Name",:surname=>nil,:email=>nil}
irb(main):003:0> a.email = 'user@example.com'
=> "user@example.com"
irb(main):004:0> a
=> #<PurchaseForm:0x00000002606b50 @name="Name",@email="user@example.com">
irb(main):005:0> a.to_hash
=> {:name=>"Name",:email=>"user@example.com"}

更重要的是,如果要使此行为可重用,请考虑将.attributes和#to_hash方法提取到单独的模块中:

module AttributesHash
  extend ActiveSupport::Concern

  class_methods do
    def attr_accessor(*args)
      @attributes = args
      super(*args)
    end

    def attributes
      @attributes
    end
  end

  included do
    def to_hash
      self.class.attributes.inject({}) do |hash,key|
        hash.merge({ key => self.send(key) })
      end
    end
  end
end

现在,只需将它包含在您的模型中即可完成:

class PurchaseForm
  include ActiveModel::Model
  include AttributesHash

  attr_accessor :name,:email

  # your validations
end

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...