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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:34:14+00:00 2026-06-18T09:34:14+00:00

I am trying to use the accepts_nested_attributes_for method within my model, however I need

  • 0

I am trying to use the accepts_nested_attributes_for method within my model, however I need to render the records grouped by another association. I have got this to work but the method I have used seems like a bit of a hack.

Is there a better way to structure this?

My Model

  has_many :quantities
  has_many :ingredients, :through => :quantities, :uniq => true
  has_many :sizes, :through => :quantities, :uniq => true
  has_many :photos, :as => :imageable

  accepts_nested_attributes_for :quantities

My View

<%= form_for [:admin, @recipe] do |f| %>

    <div class="field">
      <%= f.label :name %><br />
      <%= f.text_field :name %>
    </div>

    <% @recipe.quantities.group_by(&:size).each do |size, quantities| %>
        <h3><%= size.name %></h3>
        <%= f.fields_for :quantities do |builder| %>
            <% if builder.object.size == size %>
          <p>
            <%= builder.text_area :value, :rows => 1 %>
          </p>
          <% end %>
      <% end %>
  <% end %>

    <div class="actions">
        <%= f.submit %>
    </div>

<% end %>
  • 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-18T09:34:15+00:00Added an answer on June 18, 2026 at 9:34 am

    You can get rid of the if builder.object.size == size part with this :

    <% @recipe.quantities.group_by(&:size).each do |size, quantities_for_size| %>
      <h3><%= size.name %></h3>
      <%= f.fields_for :quantities, quantities_for_size do |builder| %>
        <p><%= builder.text_area :value, :rows => 1 %></p>
      <% end %>
    <% end %>
    

    passing the quantities_for_size as a second argument to fields_for should make it use it instead of the whole quantities associated to the recipe. See the docs on #fields_for for more information.

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

Sidebar

Related Questions

I'm trying to use accepts_nested_attributes_for on a has_one association model, and getting absolutely nowhere
I am trying to use the accepts_nested_attributes_for method based on the tutorial by Ryan
I am trying use gem tire to search in my application. I have tables
Hi I'm trying use a datepicker on a field I have. I'm trying to
I am need paint my image. I'm trying use JQuery in here this link:
So I have model with an association and I'm using a single form with
I have a class which I am trying to use to test HashMap and
I am learning RoR and trying to use accepts_nested_attributes_for and has_and_belongs_to_many to submit information
I am trying to use nested attributes on my user model for settings so
I am trying to use an external program from within Scala that accepts its

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.