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 8800669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:39:03+00:00 2026-06-14T00:39:03+00:00

I have a typical elementary problems which I cannot find a nice solution for.

  • 0

I have a typical elementary problems which I cannot find a nice solution for. Let’s take the typical many to many relationship in Rails, with the join table used to assign a value:

Recipe
    has_many :recipe_ingredients
    has_many :ingredients, :through => :recipe_ingredients
    accepts_nested_attributes_for :recipe_ingredients

Ingredient
    has_many :recipe_ingredients
    has_many :recipes, :through => :recipe_ingredients

RecipeIngredient
    belongs_to :recipe
    belongs_to :ingredient
    attr_accessible :quantity

Nothing strange here. Let’s say now I want to to create a new recipe with new ingredients all in one go. I have some JSON sent to the server like this:

{"recipe"=>
    {"name"=>"New Recipe", 
     "ingredients"=>
        [{"name" => "new Ingr 1", "quantity"=>0.1},
         {"name" => "new Ingr 2", "quantity"=>0.7}]
    }
}

I reckon I can navigate the params and create the objects one by one but I was looking into leveraging the many-to-many association and above all the accepts_nested_attributes_for, thus being able to do something like Recipe.create(params[:recipe]) and have the object tree created for me. If that means changing the JSON being sent, it is not a problem. 🙂

  • 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-14T00:39:04+00:00Added an answer on June 14, 2026 at 12:39 am

    The JSON key should be "ingredients_attributes":

    Recipe
      has_many :recipe_ingredients
      has_many :ingredients, :through => :recipe_ingredients
      attr_accessible :ingredients
      accepts_nested_attributes_for :ingredients # note this
    

    and

    {"recipe"=>
      {"name"=>"New Recipe", 
        "ingredients_attributes"=>
          [{"name" => "new Ingr 1", "quantity"=>0.1},
           {"name" => "new Ingr 2", "quantity"=>0.7}]
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a typical Parent contains many Children (List) relationship. How do I tell
I have a typical many-to-many relationship using has_many => :through , as detailed below.
So I have typical 1:M relationship: Car can have many Models I want to
I have a typical search facility in my app which returns a list of
I have a typical problem which is like the following: 1) I was working
I have a typical HABTM relationship between members (actual members of our organization, not
I have some typical codes which used HttpURLConnection to get a file with an
I have this typical problem with select. First let me make it clear that
I have a typical UITableView which displays a thumbnail image and some text. During
I have typical web application based on PHP, HTML, and javascript. From an HTML

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.