Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6161333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:27:38+00:00 2026-05-23T21:27:38+00:00

I’m trying to get a nested form view to update properly. This is however

  • 0

I’m trying to get a nested form view to update properly. This is however causing problems when the second form has existing data.

I’m using accepts_nested_attributes_for and nested_form_for. The second which only purpose is to dynamically add the form element using js. See github for more

The error I’m getting is:

Couldn't find Muscle with ID=3685340 for Exercise with ID=212831413

I’ve tried to manually do the updating but my code didnt really work and I’m under the impression that it shouldnt be needed since rails is suppose to take care of it under the hood.

The idea is that: Exercises has many Muscles through Targets
And from within the Exercise form I want to be able to add target muscles.

My models:

class Exercise < ActiveRecord::Base
  has_many :targets, :dependent => :destroy
  has_many :muscles, :through => :targets
  accepts_nested_attributes_for :muscles, :reject_if => :all_blank
  ...
end

class Target < ActiveRecord::Base
  belongs_to :exercise
  accepts_nested_attributes_for :exercise, :update_only => true
  belongs_to :muscle
end

class Muscle < ActiveRecord::Base
  has_many :targets, :dependent => :destroy
  has_many :exercises, :through => :targets
end

My (haml) view:

%p
  %b Target(s):
  = f.fields_for :muscles do |e|
    = e.collection_select :id, Muscle.all, :id, :name
    = e.link_to_remove "-remove"
  = f.link_to_add "Add target muscle", :muscles

And finally my failing controller:

  def update
    @exercise = Exercise.find(params[:id])
    @exercise.user = current_user

    params[:exercise][:muscles_attributes].each { |id, muscle|
    target = Target.where(:exercise_id => @exercise.id , :muscle_id => muscle[:id]).first
    if target && !(muscle[:_destroy] == "false")
      puts "!!>>>>>>>>>>>>>>>>>>destroy target #{target.exercise_id} #{target.muscle_id}"
      target.destroy
    else
      if !target
        t = @exercise.targets.build(:muscle_id => muscle[:id])
        t.save
      end
    end
  }

  respond_to do |format|
    if @exercise.update_attributes(params[:exercise])
      format.html { redirect_to(@exercise, :notice => 'Exercise was successfully updated.') }
      format.xml  { head :ok }
    else
      format.html { render :action => "edit" }
      format.xml  { render :xml => @exercise.errors, :status => :unprocessable_entity }
    end
  end
end

Please let me know if I should expose more of my code (the final result will be opensource anyway) ill happily push to github or whatever is requested, thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:27:40+00:00Added an answer on May 23, 2026 at 9:27 pm

    You could achieve this by manipulating the intermediate table, here targets.

    Insert in your model:

    accepts_nested_attributes_for :targets
    

    And in your html, append this line with javascript:

    <input name="exercise[targets_attributes][0][muscle_id]" value="the_muscle_id_goes_here" type="hidden">
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.