# Mutiple models in one single view?

**URL:** <https://www.ruby-forum.com/t/mutiple-models-in-one-single-view/116071>\
**Category:** Rails\
**Created:** [October 2, 2007, 2:46am UTC](https://www.ruby-forum.com/t/mutiple-models-in-one-single-view/116071 "2007-10-02T02:46:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hebertodelrio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/71e660/32.png) [@hebertodelrio](https://www.ruby-forum.com/u/hebertodelrio)\
**Post date:** [October 2, 2007, 2:46am UTC](https://www.ruby-forum.com/t/mutiple-models-in-one-single-view/116071/1 "2007-10-02T02:46:37Z")

</div>

Hi, I am a newbie in RoR, really newbie, I have followed some of the  
tutorials, and now it is time for me to start one of my own.

To simplify the set-up, I have a client that has let us say a  
pharmacy, the problem boils down to having three tables, BRAND, ACTIVE  
INGREDIENT, and MEDICAL CONDITION. These three table are related to  
each other. A brand can have many products each having only one active  
ingredient, but each active ingredient treats different medical  
conditions. (the example is not quite exact, because why should a  
brand carry more than one medicine with the same active ingredient,  
but just follow me for the sake of the example).

I want to have in one view three pulldown menus one for BRAND, one for  
ACTIVE INGREDIENT, and one for MEDICAL CONDITION.

When someone opens this view, all the three pulldowns are initialized  
to a default value. The ideas is the following as soon as one of the  
pulldowns is changed from its default value the possible list of  
options of the other two pulldown menus get constrained by the  
relationship between the three tables.

If one chooses brand WD, then maybe this brand do not carry all  
potential active ingredients, therefore it does not treat all  
potential medical condition.

If one chooses a medican condition, maybe brand PX does not have a  
medicine that treats that condition.

How do I do this?

I need help!!!

Heberto del Rio

---

<div class="post-metadata">

**Author:** ![hebertodelrio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/71e660/32.png) [@hebertodelrio](https://www.ruby-forum.com/u/hebertodelrio)\
**Post date:** [October 2, 2007, 3:10am UTC](https://www.ruby-forum.com/t/mutiple-models-in-one-single-view/116071/2 "2007-10-02T03:10:00Z")

</div>

On 2 Oct 2007, at 01:45, hebertodelrio wrote:

> brand carry more than one medicine with the same active ingredient,  
> but just follow me for the sake of the example).

You can use a field observer (see the observe\_field helper) to  
trigger an ajax call every time the selection changes. if you’ve got  
your has\_manys and has\_many :through relationships set up properly it  
should reasonably straight forward from here.

eg  
class Brand \< ActiveRecord::Base  
has\_many :products  
has\_many :active\_ingredients, :through =\> :products  
has\_many :medical\_conditions, :through =\> :active\_ingredients  
end

class MedicalCondition  
has\_many :active\_ingredient\_medical\_conditions  
has\_many :active\_ingredients, :through  
=\> :active\_ingredient\_medical\_conditions  
has\_many :products, :through =\> :active\_ingredients  
end

having picked a given brand, you can then restrict the active  
ingredients list to brand.active\_ingredients etc, or given a medical  
condition, medical\_condition.products is the list of those products  
that can treat it.

Fred
