# Virtual Attribute Model Validation

**URL:** <https://www.ruby-forum.com/t/virtual-attribute-model-validation/178859>\
**Category:** Rails\
**Created:** [November 12, 2009, 1:37pm UTC](https://www.ruby-forum.com/t/virtual-attribute-model-validation/178859 "2009-11-12T13:37:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![icke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e19adc/32.png) [@icke](https://www.ruby-forum.com/u/icke)\
**Post date:** [November 12, 2009, 1:37pm UTC](https://www.ruby-forum.com/t/virtual-attribute-model-validation/178859/1 "2009-11-12T13:37:24Z")

</div>

I have an Order model with two virtual attributes for account number  
and sort code for setting up a recurring bank payment (Direct Debit in  
the UK). I do not want to save the account details in my database  
(similar to the reason for credit card numbers) but obviously collect  
the details through a form.

I have tried this:

validates\_numericality\_of :dd\_account, :only\_integer =\> true  
validates\_length\_of :dd\_account, :is =\> 8, :wrong\_length =\>  
“^Account number|Must be 8 digits long”

but I get the following exception:

/Users/owain/.gem/ruby/1.8/gems/activerecord-2.3.4/lib/active\_record/  
attribute\_methods.rb:255:in `method_missing' /Users/owain/.gem/ruby/1.8/gems/activerecord-2.3.4/lib/active_record/ validations.rb:1019:in`send’  
/Users/owain/.gem/ruby/1.8/gems/activerecord-2.3.4/lib/active\_record/  
validations.rb:1019:in `validates\_numericality\_of\_without\_reflection’

which I suspect is that this type of validation will not work with  
virtual attributes. Is this the case???

Is the approach below the best solution or even work?

validate :custom\_validation

def custom\_validation  
errors.add(:dd\_account, “Some error message”) if detect\_something  
end

Additionally, can you use the class methods in custom methods? I  
would like to use validates\_length\_of etc if I go down the custom  
method route. Again, if I do manage to use them will they work on  
virtual attributes?

Thanks,

O.

---

<div class="post-metadata">

**Author:** ![icke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e19adc/32.png) [@icke](https://www.ruby-forum.com/u/icke)\
**Post date:** [November 12, 2009, 2:13pm UTC](https://www.ruby-forum.com/t/virtual-attribute-model-validation/178859/2 "2009-11-12T14:13:41Z")

</div>

> def custom\_validation  
> errors.add(:dd\_account, “Some error message”) if detect\_something  
> end

You can do this part. How about using the class methods so I can  
avoid resorting to this?

def dd\_details  
errors.add(:dd\_account, “^Account number|Must be 8 digits long”)  
unless dd\_account.to\_s.length == 8  
end

Ok, that’s an easy one but some of the others are a little more tricky  
and I would like to use them.

---

<div class="post-metadata">

**Author:** ![icke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e19adc/32.png) [@icke](https://www.ruby-forum.com/u/icke)\
**Post date:** [November 12, 2009, 6:02pm UTC](https://www.ruby-forum.com/t/virtual-attribute-model-validation/178859/3 "2009-11-12T18:02:00Z")

</div>

On Nov 12, 12:36 pm, icke [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> /Users/owain/.gem/ruby/1.8/gems/activerecord-2.3.4/lib/active\_record/  
> attribute\_methods.rb:255:in `method_missing' /Users/owain/.gem/ruby/1.8/gems/activerecord-2.3.4/lib/active_record/ validations.rb:1019:in `send’  
> /Users/owain/.gem/ruby/1.8/gems/activerecord-2.3.4/lib/active\_record/  
> validations.rb:1019:in `validates\_numericality\_of\_without\_reflection’
> 
> which I suspect is that this type of validation will not work with  
> virtual attributes. Is this the case???

That error is getting raised because the validation is trying to call  
attribute\_name\_before\_type\_cast  
If you implement that method it might just work.

Fred
