Mutiple models in one single view?

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs