# Habtm, multi select, and validation

**URL:** <https://www.ruby-forum.com/t/habtm-multi-select-and-validation/63949>\
**Category:** Rails\
**Created:** [June 20, 2006, 8:08pm UTC](https://www.ruby-forum.com/t/habtm-multi-select-and-validation/63949 "2006-06-20T20:08:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cleaner416](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d26b3c/32.png) [@cleaner416](https://www.ruby-forum.com/u/cleaner416)\
**Post date:** [June 20, 2006, 8:08pm UTC](https://www.ruby-forum.com/t/habtm-multi-select-and-validation/63949/1 "2006-06-20T20:08:03Z")

</div>

Greetings -

I’m trying to find the most efficient, “Rails” way to solve the  
following problem. It’s in two parts, but I think the solution is  
somewhat related:

I have Recipe and Category model related by HABTM. I would like to

1.) Validate the _number_ of categories associated with a recipe, say a  
minimum of 1 and a max of 3. (So validates\_associated will not work  
here, right?)

2.) In the view, present a select box with a prompt in it, e.g. “Choose  
1 to 3 of the following categories”, as the first option. I’ve found  
ways to get this to work with the :prompt method in a 1-to-many  
relationship, but I haven’t found anything to work with a HABTM

I’ve solved #1 with a controller hack that feeds the number categories  
to a recipe accessor and then validating that, but that seems so …  
wrong.

Any suggestions, pointers?

TIA!

---

<div class="post-metadata">

**Author:** ![cleaner416](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d26b3c/32.png) [@cleaner416](https://www.ruby-forum.com/u/cleaner416)\
**Post date:** [June 20, 2006, 8:49pm UTC](https://www.ruby-forum.com/t/habtm-multi-select-and-validation/63949/2 "2006-06-20T20:49:44Z")

</div>

On Tuesday, June 20, 2006, at 8:08 PM, Lance D. wrote:

> here, right?)  
> Any suggestions, pointers?
> 
> TIA!
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

a custom validate method should do the trick.

If you read through the validation docs in the API, it might give you  
some info about this, and the AWDwR.

\_Kevin

---

<div class="post-metadata">

**Author:** ![cleaner416](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d26b3c/32.png) [@cleaner416](https://www.ruby-forum.com/u/cleaner416)\
**Post date:** [June 21, 2006, 10:33am UTC](https://www.ruby-forum.com/t/habtm-multi-select-and-validation/63949/3 "2006-06-21T10:33:59Z")

</div>

Just write a validate method in the Recipe class:

class Recipe \< ActiveRecord::Base  
def validate  
errors.add\_to\_base “Please select between one and three categories  
for the recipe” unless (1…3).include?(categories.size)  
end  
end

-Jonathan.

On 20 Jun 2006 18:47:30 -0000, Kevin O.
