# Validating Virtual Properties

**URL:** <https://www.ruby-forum.com/t/validating-virtual-properties/140621>\
**Category:** Rails\
**Created:** [June 16, 2008, 9:22pm UTC](https://www.ruby-forum.com/t/validating-virtual-properties/140621 "2008-06-16T21:22:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trogdor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@Trogdor](https://www.ruby-forum.com/u/Trogdor)\
**Post date:** [June 16, 2008, 9:22pm UTC](https://www.ruby-forum.com/t/validating-virtual-properties/140621/1 "2008-06-16T21:22:28Z")

</div>

Hello,

I have two virtual properties (schedule and diameter) that are saved  
in the db as one property (diameter\_schedule\_id -\> fk to another  
model). When you set the properties in the model, they locate the  
proper DiameterSchedule and save the id. The exception happens when  
the selected schedule is not available for the chosen diameter. I  
wrote a validator that correctly throws an error using  
errors.add(:schedule, …). Although the validator works correctly, it  
just raises a generic error, I would like it to highlight the field  
and display the error (like it is supposed to?). Any ideas?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![Trogdor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@Trogdor](https://www.ruby-forum.com/u/Trogdor)\
**Post date:** [June 16, 2008, 9:53pm UTC](https://www.ruby-forum.com/t/validating-virtual-properties/140621/2 "2008-06-16T21:53:51Z")

</div>

On Jun 16, 2008, at 11:17 AM, Trogdor wrote:

> I have two virtual properties (schedule and diameter) that are saved  
> in the db as one property (diameter\_schedule\_id -\> fk to another  
> model). When you set the properties in the model, they locate the  
> proper DiameterSchedule and save the id. The exception happens when  
> the selected schedule is not available for the chosen diameter. I  
> wrote a validator that correctly throws an error using  
> errors.add(:schedule, …). Although the validator works correctly, it  
> just raises a generic error, I would like it to highlight the field  
> and display the error (like it is supposed to?). Any ideas?

You can specify an error message

self.errors.add(:schedule, ‘A schedule for that diameter is not  
available.’)

And then display that error just like you would for any built-in  
validation error.

–  
def gw  
writes\_at ‘www.railsdev.ws’  
end

---

<div class="post-metadata">

**Author:** ![Trogdor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@Trogdor](https://www.ruby-forum.com/u/Trogdor)\
**Post date:** [June 16, 2008, 9:53pm UTC](https://www.ruby-forum.com/t/validating-virtual-properties/140621/3 "2008-06-16T21:53:51Z")

</div>

One other thing. The view renders forms for two different models. I  
have a form\_for for the parent and fields\_for for the child. The child  
is the one I am having trouble with, but when I put validation into  
the parent, I get the same problem. \<%= f.error\_messages %\> and \<%=  
fields.error\_messages %\> are in the appropriate spots as well.

---

<div class="post-metadata">

**Author:** ![Trogdor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@Trogdor](https://www.ruby-forum.com/u/Trogdor)\
**Post date:** [June 16, 2008, 9:55pm UTC](https://www.ruby-forum.com/t/validating-virtual-properties/140621/4 "2008-06-16T21:55:02Z")

</div>

That’s how I am doing it right now, the problem is that it won’t  
display on the form (I have tried both self.errors.add and  
errors.add).

---

<div class="post-metadata">

**Author:** ![Trogdor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@Trogdor](https://www.ruby-forum.com/u/Trogdor)\
**Post date:** [June 16, 2008, 10:19pm UTC](https://www.ruby-forum.com/t/validating-virtual-properties/140621/5 "2008-06-16T22:19:50Z")

</div>

could it be cause you have to use errors.add\_to\_base?

I’m guessing, but I think it might work.

---

<div class="post-metadata">

**Author:** ![Trogdor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5ac83/32.png) [@Trogdor](https://www.ruby-forum.com/u/Trogdor)\
**Post date:** [June 16, 2008, 10:50pm UTC](https://www.ruby-forum.com/t/validating-virtual-properties/140621/6 "2008-06-16T22:50:02Z")

</div>

Thanks for your help, but now it just ignores the error altogether.

I tried using errors.add(:field, “description”) in another controller  
without a multi-model form and it worked as expected.
