Rails 2.3.11 Creating a Model for a Form and Using ActiveRecord Validation

In Rails 3, you simply enable ActiveRecord modules to add validations for any model not supported by the database. I want to create a model for the form (for example, the ContactForm model) and enable ActiveRecord validations. But you cannot just enable ActiveRecord modules in Rails 2.3.11. Is there a way to accomplish the same behavior as Rails 3 in Rails 2.3.11?

+3
source share
1 answer

If you just want to use the virtual class as your proxy server for several models, this can help (for 2.3.x, 3.xx you can use ActiveModel for users, as mentioned earlier):

class Registration
  attr_accessor :profile, :other_ar_model, :unencrypted_pass, :unencrypted_pass_confirmation, :new_email
  attr_accessor :errors

  def initialize(*args)
    # Create an Errors object, which is required by validations and to use some view methods.
    @errors = ActiveRecord::Errors.new(self)
  end

  def save
    profile.save
    other_ar_model.save
  end
  def save!
    profile.save!
    other_ar_model.save!
  end

  def new_record?
    false
  end

  def update_attribute
  end
  include ActiveRecord::Validations
  validates_format_of :new_email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
  validates_presence_of :unencrypted_pass
  validates_confirmation_of :unencrypted_pass
end

Validations, , save save! , . , , .

+2

All Articles