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

  • SEARCH
  • Home
  • 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 8126843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:18:11+00:00 2026-06-06T07:18:11+00:00

I am trying to send a POST request to a rails scaffold controller which

  • 0

I am trying to send a POST request to a rails scaffold controller which contains a nested array representing records that need to be created and associated with the newly created parent.

Here is some example JSON:

{
    "plan_id":3,
    "weight":60,
    "exercise_sets": [
        {
            "created_at":"2012-06-13T14:55:57Z",
            "ended_at":"2012-06-13T14:55:57Z",
            "weight":"80.0",
            "repetitions":10,
            "exercise_id":1
        }
    ]
}

..and my models..

class Session < ActiveRecord::Base
  has_many :exercise_sets, :dependent => :destroy
  has_many :exercises, :through => :exercise_sets
end

class ExerciseSet < ActiveRecord::Base
  belongs_to :exercise
  belongs_to :session
end

Is what I am attempting possible?

  • 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-06-06T07:18:12+00:00Added an answer on June 6, 2026 at 7:18 am

    This is certainly not impossible, though you may have to switch up your parameter naming a bit.

    When you pass the JSON above to the controller, it either gets passed as parameters to a constructor:

    Session.new(params[:session])
    

    Or gets passed to the #update_attributes method on a persisted Session instance:

    @session = Session.find(params[:id])
    @session.update_attributes(params[:session])
    

    Both the constructor and #update_attributes methods turn parameters like “plan_id” into assigment method calls. That is,

    @session.update_attributes(:plan_id => "1")  
    

    Turns into (inside the #update_attributes method):

    @session.plan_id = "1"
    

    So, this works for your plan_id and weight attributes, because you have both #plan_id= and #weight= setter methods. You also have an #exercise_sets= method given to you by has_many :exercise_sets. However, the #exercise_sets= method expects ExerciseSet objects, not ExerciseSet attributes.

    Rails is capable of doing what you are trying to do via the #accepts_nested_attributes_for class method. Try this:

    class Session < ActiveRecord::Base
      has_many :exercise_sets, :dependent => :destroy
      has_many :exercises, :through => :exercise_sets
    
      accepts_nested_attributes_for :exercise_sets
    end
    

    This sets up (metaprograms) an #exercise_sets_attributes= method for you. So just modify your JSON to:

    {
        "plan_id":3,
        "weight":60,
        "exercise_sets_attributes": [
            {
                "created_at":"2012-06-13T14:55:57Z",
                "ended_at":"2012-06-13T14:55:57Z",
                "weight":"80.0",
                "repetitions":10,
                "exercise_id":1
            }
        ]
    }
    

    More info: http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to send a POST request to my Ruby on Rails local
I`m trying to make my Go server send POST request with an array of
I need to send a POST request using java. The server i am trying
I'm trying to send a post request to Parse: https://parse.com/docs/rest#objects-creating . Can't get it
I have a problem when trying to send a POST request. The sending method
Trying to find a way to send a POST HTTPS request from Python to
Trying to send POST request on click event using jQuery with no luck. Here
While trying to send a POST request via xmlhttp.open(POST, url, true) (javascript) to the
i'm trying to use ASIFormDataRequest to send post request. But when I import the
I am trying to output POST-data that I send from an android phone, however

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.