ruby - Rails: Add attribute values to several new records in a join table in a has_many :through association -
i have form creates new exercise showing muscle groups worked. here's example of want enter db:
new exercise: name => pull-up, primary => back, secondary => [biceps, forearms, chest]
how should set up? don't want store primary , secondary arrays in muscle_groups_exercised table because have future queries search exercises based on muscle_group primary exercise.
here schema part of app:
create_table "exercises", force: true |t| t.string "name" t.datetime "created_at" t.datetime "updated_at" end create_table "muscle_groups", force: true |t| t.string "name" t.datetime "created_at" t.datetime "updated_at" end create_table "muscle_groups_exercised", force: true |t| t.integer "muscle_group_id" t.integer "exercise_id" t.binary "primary" t.binary "secondary" end add_index "muscle_groups_exercised", ["exercise_id"], name: "index_muscle_groups_exercised_on_exercise_id", using: :btree add_index "muscle_groups_exercised", ["muscle_group_id"], name: "index_muscle_groups_exercised_on_muscle_group_id", using: :btree
here models:
class exercise < activerecord::base has_many :muscle_groups, through: :muscle_groups_exercised end class musclegroup < activerecord::base has_many :exercises, through: :muscle_groups_exercised end class musclegroupexercised < activerecord::base belongs_to :exercise belongs_to :muscle_group end
i have exercises_controller , muscle_groups_controller. think code should reside in exercises_controller , muscle_groups_exercised model not sure how this.
what doing looks right me. imagine want apply muscle groups exercise, if going searching within muscle groups exercises later on. put required code within exercises_controller.rb
.
check out ryan bates article , video on hambtm checkboxes on how this. isn't doing, need apply additional value secondary or primary.
btw, wouldn't have 2 columns secondary , primary, since there 2 values, have primary 1 , assume if isn't true, muscle group secondary.
Comments
Post a Comment