Saving many_to_many

Ahoy, i’m trying to save a many to many between “talent” and “vital
stat”

talent_controller.rb

def edit
@talent = Talent.find(params[:id], :include => [:talent_type,
:vital_stats])
@talent_types = TalentType.find_all
@vital_stats = VitalStat.find_all

if request.post?
  @talent.attributes = params[:talent]
  @talent.attributes = params[:talent_type]
  if @talent.update_attributes(params[:vital_stat][:id])
    flash[:notice] = 'Talent was successfully updated.'
  end
end

end

def delete

Form fields for checkboxes

Select Vital Stats
<% VitalStat.find(:all).each do |g| %>

   <%= form_bit_checkbox('vital_stat', g.id, g.title, '', '',

@talent.vital_stats.include?(g) ? {‘checked’ => ‘checked’} : ‘’) %>
<% end %>

My problem is that the talent isn’t being updated w/ the checked or
unchecked fields. The returned PARAMS could look like this:

commit: Save
action: edit
talent: !map:HashWithIndifferentAccess
title: Casting Director
id: “2”
talent_type: !map:HashWithIndifferentAccess
talent_type_id: “1”
controller: talent
vital_stat: !map:HashWithIndifferentAccess
“2”: “0”
“3”: “0”
“4”: “1”

which means there are three checkboxes, the last being checked - but my
talent always shows all 3 as checked (meaning its not updating the fact
that they are currently unchecked)

Any help?

sorry for the lot of code, but the question is very simple. Anyone?

if @talent.update_attributes(params[:vital_stat][:id])

Aren’t you missing a .save there?

doesn’t work either way
Here is the debug of params:

— !map:HashWithIndifferentAccess
commit: Save
action: edit
talent: !map:HashWithIndifferentAccess
title: Casting Director
id: “2”
talent_type: !map:HashWithIndifferentAccess
talent_type_id: “1”
controller: talent
vital_stat: !map:HashWithIndifferentAccess
“2”: “0”
“3”: “0”
“4”: “1”

^ notice the vital stat ID’s, 0 = not set, 1 = set.
talent debug:

— &id001 !ruby/object:Talent
attributes:
title: Casting Director
id: “2”
talent_type_id: “1”
errors: !ruby/object:ActiveRecord::Errors
base: *id001
errors: {}

new_record_before_save:
talent_type: !ruby/object:TalentType
attributes:
title: Talent
id: “1”
vital_stats:

  • !ruby/object:VitalStat
    attributes:
    title: Hair Color
    id: “2”
  • !ruby/object:VitalStat
    attributes:
    title: Eye Color
    id: “3”
  • !ruby/object:VitalStat
    attributes:
    title: Height
    id: “4”

Still shows association with all vital stats.

if @talent.update_attributes(params[:vital_stat][:id])

Aren’t you missing a .save there?

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