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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:41:34+00:00 2026-06-17T16:41:34+00:00

i’ve been searching through similar questions but i still don’t get how implement this

  • 0

i’ve been searching through similar questions but i still don’t get how implement this relationship. I have of course three models :

class Recetum < ActiveRecord::Base
    attr_accessible :name, :desc, :duration, :prep, :photo, :topic_id
    has_many :manifests
    has_many :ingredients, :through => :manifests
end

class Ingredient < ActiveRecord::Base
  attr_accessible :kcal, :name, :use, :unity
  has_many :manifests
  has_many :recetum, :through => :manifests
end


class Manifest < ActiveRecord::Base
  attr_accessible :ingredient_id, :quantity, :receta_id
  belongs_to :recetum
  accepts_nested_attributes_for :ingredient
  belongs_to :ingredient
end

Recetum would be a recipe (typo when scaffolding), this recipe may have one or more ingredients (already on the db). So when i create a new Recetum, i need the new recetum to be created and one record inserted in manifest for each ingredient entered by the user.

I would need some help now with views and controllers, how do i create the form for recetum with fields for the ingredients and more important what do i have to modify recetum controller.

Any suggestions or help would be very much appreciated as this part is crucial for my project, 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-06-17T16:41:35+00:00Added an answer on June 17, 2026 at 4:41 pm

    You have a couple options, and mainly they depend on what you want to do in your view. Do you want to display a set number of max_ingredients or do you want it to be completely dynamic? The dynamic case looks better for the user for sure, but it does make for some more complicated code.

    Here is a good RailsCast which explains how to do it dynamically via JavaScript:

    http://railscasts.com/episodes/74-complex-forms-part-2

    Unfortunately, not everyone runs with JavaScript enabled so you may want to consider doing it the static way.

    Firstly, I don’t think you need accepts_nested_attributes_for in your Manifest model. However, I do think you need it in your Recetum model. If you’re going the static route, you’ll probably want to set a reject_if option too.

    accepts_nested_attributes_for :manifests, reject_if: :all_blank
    

    Once you do this, you’ll need to add manifests_attributes to your attr_accessible.

    With the static route, you’ll need to prebuild some of the manifests. In your new controller you’ll want something like this:

    max_ingredients.times do
      @recetum.manifests.build
    end
    

    In your edit and the error paths of your create and update, you may want:

    (max_ingredients - @recetum.manifests.count).times do
      @recetum.manifests.build
    end
    

    Finally, your view will need some way to set the ingredient. I’ll assume a select box for now.

    f.fields_for :manifests do |mf|
      mf.label :ingredient_id, "Ingredient"
      mf.collection_select :ingredient_id, Ingredient.all, :id, :name
    

    You’ll want to add some sort of formatting through a list or table probably.

    Hopefully, that’s enough to get you started.

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

Sidebar

Related Questions

This could be a duplicate question, but I have no idea what search terms
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this
I don't have much knowledge about the IPv6 protocol, so sorry if the question

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.