adding a level in has many association rails -
i have association in rails application between states , hospitals. state has many hospitals , hospital belongs 1 state. so,
class state < activerecord::base has_many :hospitals end class hospital < activerecord::base belongs_to :state end
so, have state_id column in hospitals table. now, due requirement, need add level of hierarchy in association ie. new model city. such that,a state has many cities, , city has many hospitals, so, state has many hospitals.
my models should -
class state < activerecord::base has_many :cities has_many :hospitals, through: :cities end class hospital < activerecord::base belongs_to :city end class city < activerecord::base has_many :hospitals belongs_to :state end
will such change in models work, if want run queries @state.hospitals
also, application has data populated state 1....n hospitals
association using state_id
column. how go add model in between , keep data sane ??
i'm new rails , appreciated. thanks.
Comments
Post a Comment