associations - How can I set up a static form for adding associated records in Rails? -
i have following models i'm using...
game
class game < activerecord::base has_many :workout has_many :participations end
participation
class participation < activerecord::base belongs_to :player belongs_to :game end
workout
class workout < activerecord::base belongs_to :player belongs_to :game has_many :measurables end
measurable
class measurable < activerecord::base belongs_to :workout end
workout
nested resource player
. usually, create workout
record on player
1 @ time. add measurable
s workout
using gem nested_fields_for allow me add/remove 1 measurable @ time, 1 player @ time. i'm trying build form user edit multiple measurables every player participating in game.
example form
player | height | weight | hand span | player 1's name [input box] [input box] [input box] player 2's name [input box] [input box] [input box] player 3's name [input box] [input box] [input box] player 4's name [input box] [input box] [input box]
i've far created view under views/games directory edit_measurables.rb
, action in games_controller
called #edit_measurables. planning create_or_find workout
corresponds game
here , build form shown above. though i'm not sure go here. has me thinking have incorrectly organized models , associations.
update:
i've managed little further along, i'm still running few issues, albeit outside scope of question.
the form i'm wanting construct i'm trying handle in following way...
<table> <thead> <tr> <th>player</th> <th>height</th> <th>weight</th> <th>hand</th> </tr> </thead> <tbody> <% @game.participations.each |participant| %> <% player = participant.player %> <% simple_form_for participant.player |player_form| %> <% workout = workout.find_or_create_by(...) %> <% @measurables.each {|m| workout.measurables.build(...) unless workout.measurables.any? { ... } } %> <tr> <td><%= player.full_name %></td> <% player_form.nested_fields_for :measurables, workout.measurables, wrapper_tag: :td |measurable_form| %> <td><%= measurable_form.input :value, label: false %></td> <% end %> </tr> <% end %> </tbody> </table>
<% game.participations.each |participation| %> <% workout = participation.game.workout %> <%= form_for participation.player |player_form| %> <%= fields_for player.measureables.where(workout: workout) |measurable_form| %> <%= measurable_form.text_field :height %> <% end %> <% end %> <% end %>
so you're doing giving player
set of measurables
each workout
, of game
has one?
the problem here you've not associated measurables
game
& player
, through participations
. think why you're seeing problem; measurables
have pulled unrelated table.
if case, why not put measurables in participation
model?
the "has_one" workout mean 1 workout game... means game belong_to
workout?? if case, participating in game able store measurables that game in participation
model...
--
#app/views/games/edit.html.erb <%= form_for @game |f| %> <%= f.fields_for :participations |p| %> <%= p.object.player.name unless p.object.new_record? %> <%= p.collection_select :player_id, player.all, :id, :name if p.object.new_record? %> <%= p.number_field :measurable_height %> <%= p.number_field :measurable_weight %> <%= p.number_field :measurable_hand_span %> <% end %> <%= f.submit %> <% end %>
this allow associate measurables
directly:
#app/models/game.rb class game < activerecord::base belongs_to :workout has_many :participations has_many :players, through: :participations end #app/models/participation.rb class participation < activerecord::base #id | player_id | game_id | measurable_height | measurable_weight | measurable_hand_span | created_at | updated_at belongs_to :player belongs_to :game end
with following controller:
#app/controllers/games_controller.rb class gamescontroller < applicationcontroller def new @game = game.new @game.participations.build #-> needed "add" fields end def edit @game = game.find params[:id] end def update @game = game.find params[:id] @game.update update_params end private def update_params params.require(:game).permit(partipations_attributes: [:measurable_height, :measurable_weight, :measurable_hand_span]) end end
Comments
Post a Comment