ruby - Rails associations has_many through instead of HABTM -
i'm new rails , activerecord , i'm trying find correct way model data.
i building application let's swim instructors put class plan shows skills teaching class , activities use teach each skill. plan can contain many skills , each skill can have many activities associated it.
on plan form there widget skill-activity combination. in it, user should able select skill dropdown , selected skill select multiple activities list. widget can repeat number of times on form.
my current model:
class plan has_many :plan_activities end class planactivities belongs_to :plan belongs_to :skill has_and_belongs_to_many :activities end class skill end class activity end
is model correct? problem accepts_nested_attribtues_for not work habtm associations. i've read can replace has_many through:, mean adding yet join model picture. seems little ugly. there better way this?
edit:
my skills , activities in list form , should able include same skill and/or activity on multiple plans.
your model close. 1 improvement here remove planactivities model. have associations directly on plan. @ point don't think model justified. have pretty basic pyramid structure here. @ top plan many skills, each skill has many activities, simpler model like:
class plan has_many :plan_skills has_many :activities, through: :plan_skills end class skill has_many :activities, through: plan_skills has_many :plan_skills end class planskill belongs_to :plan belongs_to :skill has_many :activities end class activity belongs_to :plan_skill end
the join model option not entirely necessary here. plan.activities make needed join table you.
Comments
Post a Comment