# Validate select list

**URL:** <https://www.ruby-forum.com/t/validate-select-list/140270>\
**Category:** Rails\
**Created:** [June 12, 2008, 1:48pm UTC](https://www.ruby-forum.com/t/validate-select-list/140270 "2008-06-12T13:48:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![eddy\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@eddy\_b](https://www.ruby-forum.com/u/eddy_b)\
**Post date:** [June 12, 2008, 1:48pm UTC](https://www.ruby-forum.com/t/validate-select-list/140270/1 "2008-06-12T13:48:55Z")

</div>

Hi,

I have two database - tables (users and user\_permissions) which have a  
1…n - relationship. If I want to create a new entry on ‘users’ table I  
need the correct user\_permission\_id from the other table. So the form  
which I use has a select list with all entries of the user\_permissions -  
table but if anybody insert an invalid key into the select list I get  
inconsistent data in my database. How can I solve this problem?

At this time I use “validates\_inclusion\_of” and “validates\_associated”  
to solve this problem but without any success.

Eddy

---

<div class="post-metadata">

**Author:** ![eddy\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@eddy\_b](https://www.ruby-forum.com/u/eddy_b)\
**Post date:** [June 12, 2008, 2:14pm UTC](https://www.ruby-forum.com/t/validate-select-list/140270/2 "2008-06-12T14:14:59Z")

</div>

Hi Eddy,

Eddy B. wrote:

> I have two database - tables (users and user\_permissions)  
> which have a 1…n - relationship. If I want to create a new  
> entry on ‘users’ table I need the correct user\_permission\_id  
> from the other table. So the form which I use has a select  
> list with all entries of the user\_permissions - table but if  
> anybody insert an invalid key into the select list I get  
> inconsistent data in my database. How can I solve this  
> problem?

The ‘standard’ approach to this is to put an ‘observe\_field’ on the  
first  
select field which fires a method that, based on the value selected,  
re-renders (typically with RJS) the second select. If you’re new to  
RJS,  
Cody F.'s piece at O’Reilly is we’ll worth the $10.

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![eddy\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@eddy\_b](https://www.ruby-forum.com/u/eddy_b)\
**Post date:** [June 12, 2008, 4:24pm UTC](https://www.ruby-forum.com/t/validate-select-list/140270/3 "2008-06-12T16:24:56Z")

</div>

Hi Bill,

thanks for your post but I looking for a solution for my ‘user’ model  
which allows me to validate the user\_permission\_id with all entry of the  
user\_permissions - table. Has anybody a solution or a tip for my  
problem?

Eddy

---

<div class="post-metadata">

**Author:** ![eddy\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@eddy\_b](https://www.ruby-forum.com/u/eddy_b)\
**Post date:** [June 12, 2008, 4:51pm UTC](https://www.ruby-forum.com/t/validate-select-list/140270/4 "2008-06-12T16:51:15Z")

</div>

On 12 Jun 2008, at 12:48, Eddy B. wrote:

> inconsistent data in my database. How can I solve this problem?

I would just use a foreign key.

Fred
