# Dry collections in other models

**URL:** <https://www.ruby-forum.com/t/dry-collections-in-other-models/183029>\
**Category:** Rails\
**Created:** [February 6, 2010, 12:59pm UTC](https://www.ruby-forum.com/t/dry-collections-in-other-models/183029 "2010-02-06T12:59:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 6, 2010, 12:59pm UTC](https://www.ruby-forum.com/t/dry-collections-in-other-models/183029/1 "2010-02-06T12:59:48Z")

</div>

Starting over, mail program sent prematurely…

2 models, Salons, Reviews

In ApplicationHelper.rb…

SALON\_STATES = Salon.find(:all, :group =\> ‘state’, :select =\> ‘state’)

So in any view, I can collect…

\<%= options = [[‘Select a State’, @state]] +  
ApplicationHelper::SALON\_STATES.collect { |salon| [salon.state] }  
select ‘salon’, ‘state’, options %\>

This is OK (perhaps maybe not the best way to handle and I’m open to  
suggestions)

But now, I want a list of cities in just the selected state and I can’t  
manage to accomplish this anywhere but in controller code which is not  
very dry since I would have to replicate this code in several different  
controllers (like Reviews).

@cities = Salon.find(:all, :conditions =\> [‘active = true AND state  
= ?’, @state], :group =\> ‘city’, :select =\> ‘city’)

Suggestions?

Craig

–  
This message has been scanned for viruses and  
dangerous content by MailScanner, and is  
believed to be clean.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 7, 2010, 2:13pm UTC](https://www.ruby-forum.com/t/dry-collections-in-other-models/183029/2 "2010-02-07T14:13:49Z")

</div>

Hi Craig,

I had to do something similar to this in one of my apps. Ryan B.  
did an excellent tutorial you may want to consider.

> **[\#88 Dynamic Select Menus - RailsCasts](http://railscasts.com/episodes/88-dynamic-select-menus)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

Have fun,  
Franz

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 7, 2010, 6:00pm UTC](https://www.ruby-forum.com/t/dry-collections-in-other-models/183029/3 "2010-02-07T18:00:52Z")

</div>

On Sun, 2010-02-07 at 14:12 +0100, Franz S. wrote:

> Hi Craig,
> 
> I had to do something similar to this in one of my apps. Ryan B.  
> did an excellent tutorial you may want to consider.
> 
> [#88 Dynamic Select Menus - RailsCasts](http://railscasts.com/episodes/88-dynamic-select-menus)

* * *

I may ultimately go down that road but I am playing with named\_scope  
since it’s really something I should be mastering anyway and so I will  
ask about that in another thread…

Thanks

Craig

–  
This message has been scanned for viruses and  
dangerous content by MailScanner, and is  
believed to be clean.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 8, 2010, 12:12am UTC](https://www.ruby-forum.com/t/dry-collections-in-other-models/183029/4 "2010-02-08T00:12:31Z")

</div>

On Feb 6, 6:57 am, Craig W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Starting over, mail program sent prematurely…
> 
> 2 models, Salons, Reviews
> 
> In ApplicationHelper.rb…
> 
> SALON\_STATES = Salon.find(:all, :group =\> ‘state’, :select =\> ‘state’)

Unrelated to your original question, but DON’T DO THIS. It works (for  
suitably small values of “works”) in development mode, but will cause  
odd behavior when deployed to production. At a minimum, Salons which  
are added after the application starts won’t show up until the server  
restarts. In a dynamic environment like Passenger, you could even end  
up with different processes having _different_ lists in  
SALON\_STATES…

–Matt J.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 8, 2010, 1:08am UTC](https://www.ruby-forum.com/t/dry-collections-in-other-models/183029/5 "2010-02-08T01:08:07Z")

</div>

On Sun, 2010-02-07 at 15:11 -0800, Matt J. wrote:

> Unrelated to your original question, but DON’T DO THIS. It works (for  
> suitably small values of “works”) in development mode, but will cause  
> odd behavior when deployed to production. At a minimum, Salons which  
> are added after the application starts won’t show up until the server  
> restarts. In a dynamic environment like Passenger, you could even end  
> up with different processes having _different_ lists in  
> SALON\_STATES…

* * *

yeah thanks

I’ve already moved this code into the ‘Salons’ model as a named\_scope

😉

Craig

–  
This message has been scanned for viruses and  
dangerous content by MailScanner, and is  
believed to be clean.
