0

I have this resource:

require 'active_resource'

class User < ActiveResource::Base
  self.site = 'http://localhost:3000/api/v2'
  self.include_format_in_path = false

  schema do
    attribute :id,                 :integer
    attribute :name,               :string
    attribute :password,           :string
    attribute :email,              :string
  end

  validates :email, confirmation: true
  # alternatively, tried with:
  # validates_confirmation_of :email
end

When I call:

user = User.new(
  name: 'My name',
  email: 'an@email.com',
  email_confirmation: 'another@email.com',
  password: 'some_password'
)

and then user.errors, I would expect it to return an error about the email_confirmation not matching email. However, it doesn't. user.valid? always returns true.

Moreover, user.email returns 'an@email.com', but user.email_confirmation returns nil.

I tested validating the password too, and looks like any user.xxx_confirmation will return nil.

How are we supposed to validate confirmations on ActiveResource?

Guilherme
  • 7,839
  • 9
  • 56
  • 99
  • email_confirmation shouldn't be a real attribute... http://guides.rubyonrails.org/active_record_validations.html#confirmation – Brad Werth May 11 '16 at 19:00
  • I thought so, but I tried removing it from `schema`, it still does not work :/ – Guilherme May 11 '16 at 19:03
  • @BradWerth just to make it clear, it's an `ActiveResource`, not an `ActiveRecord`. – Guilherme May 11 '16 at 19:09
  • what if you add an attr_accessor, or maybe presence validation? – Brad Werth May 11 '16 at 19:12
  • @BradWerth is correct here - you should remove the `email_confirmation` attribute from the "schema". `validates_confirmation_of` creates a setter for the `x_confirmation` attribute. http://apidock.com/rails/ActiveModel/Validations/HelperMethods/validates_confirmation_of – max May 11 '16 at 19:14
  • @max agreed. I also tried validating using `validates_confirmation_of :email`, but it doesn't work either. `user.valid?` will return `true`, and `user.errors` won't return any errors. – Guilherme May 11 '16 at 19:29
  • I'm guessing reason the validation is not running is because `email_confirmation` is nil like the documentation states. The question is really why is it nil? Also bear in mind that the `errors` object is empty until you call `.valid?` on the model which causes the validations to be run. – max May 11 '16 at 19:37
  • Perfect, understood. However, it still does not validate :/ – Guilherme May 11 '16 at 19:42

0 Answers0