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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:46:40+00:00 2026-05-25T02:46:40+00:00

For some background, please see my question: Rails app using STI — easiest way

  • 0

For some background, please see my question: Rails app using STI — easiest way to pull these records?

I have some models joined with a has_many through relationship using a third join table (Recipe and Ingredient joined through RecItem).

So the guys on the prior question helped me to group my recipe’s ingredients by type – this is exactly what I wanted. However, now that I am accessing @recipe.ingredients instead of @recipe.rec_items, I can’t get back to the data stored in the rec_items join table (stuff like amount of ingredient, how long to cook it, etc…)

Prior to using the group_by, I was iterating through each recipe’s rec_items like so:

<% @recipe.rec_items.each do |rec_item| %>
<%= rec_item.ingredient.name %><br />
<%= rec_item.amount %><br />
<% end -%>

Now that I am grouping @recipe.ingredients, how can I follow back to the rec_items related to this recipe? (If I do ingredient.rec_items it gives me rec_items for all recipes…again, I can do this with clunky statements like:

ingredient.rec_items.find_by_recipe_id(@recipe.id).amount

but that seems wrong…) Is there an easy way to accomplish these goals? (getting a list of a particular recipe’s ingredients, sorted/grouped by :type, while still being able to access the additional info in rec_items for each recipe/ingredient pairing?)

Thanks!

  • 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-25T02:46:41+00:00Added an answer on May 25, 2026 at 2:46 am

    Posting same answer again, with a little modification:

    You can use group_by here. By the way you better use includes in the query for recipe, otherwise there are going to be many queries.

    recipe.rec_items.group_by {|ri| ri.ingredient.type}.each do |type, rec_items|
      puts type
    
      rec_items.each do |rec_item|
        puts rec_item.inspect
        puts rec_item.ingredient.inspect
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please see this question for background information: How do Tasks in the Task Parallel
First some background: VB.NET 2005 Application that accesses a MS-SQL back-end, using multiple Web
Some background info; LanguageResource is the base class LanguageTranslatorResource and LanguageEditorResource inherit from LanguageResource
Some background: In Germany (at least) invoice numbers have to follow certain rules: The
Some background: I've created a Swing application which uses the Substance LaF (Thanks again,
Some background I'm currently working on a mobile site so I keep switching user
Some background: My job involves maintaining a large multi-threaded multi-process C++ / C# application,
Some Background to begin: I've implemented a custom MembershipProvider that validates a user from
Some background: we have a windows application (c#) that locate in the system try.
Some background: I'm a jack-of-all trades, one of which is programming. I learned VB6

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.