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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:40:27+00:00 2026-05-23T23:40:27+00:00

I am building a recipe manager as a first rails app. I have a

  • 0

I am building a recipe manager as a first rails app. I have a many-to-many, nested set of models based on this quite nice ERD. Ideally, I would like to create a form that allows me to create the recipe in a single form. Also, I would like the user to be able to write/paste the ingredients and steps into a single text field, respectively. In the code below, there is a virtual attribute that parses the cr separated lists in the form to attempt this.

I keep getting a “readonly” has_many through error when I write the ingredients. I understand that, based on excellent help I got offline, my join is not properly setup.

To simplify things, I would like to assign the ingredients list to either the first step or every step. How do I write the code so that it manually creates the join model with the virtual attribute?

My Four Models:

recipe.rb

    class Recipe < ActiveRecord::Base
      has_many :steps, :dependent => :destroy
      has_many :stepingreds, :through => :steps
      has_many :ingredients, :through => :stepingreds
      validates_presence_of :name, :description
      attr_writer :step_instructions, :ingredient_names
      after_save :assign_steps, :assign_ingredients

      def step_instructions
        @step_instruction || steps.map(&:instruction).join("\n")
      end

      def ingredient_names
        @ingredient_name || ingredients.map(&:name).join("\n")
      end

    private

    def assign_steps
        if @step_instructions
          self.steps = @step_instructions.split(/\n+/).map do |instruction|
            Step.find_or_create_by_instruction(instruction)
          end
        end
    end

      def assign_ingredients
        if @ingredient_names
          self.ingredients = @ingredient_names.split(/\n+/).map do |name|
            Ingredient.find_or_create_by_name(name)
          end
        end
      end
    end

step.rb

    class Step < ActiveRecord::Base
      #attr_accessible :recipe_id, :number, :instructions
      belongs_to :recipe
      has_many :stepingreds, :class_name => 'Stepingred'
      has_many :ingredients, :through => :stepingreds
    end

stepingred.rb

    class Stepingred < ActiveRecord::Base
      belongs_to :ingredient
      belongs_to :step, :class_name => 'Step'
    end

ingredient.rb

    class Ingredient < ActiveRecord::Base
      has_many :stepingreds
      has_many :steps, :through => :stepingred
      has_many :recipes, :through => :steps
    end

And here is my stripped down form:

    <%= form_for @recipe do |f| %>
      <%= f.error_messages %>
      <p>
        <%= f.label :name %><br />
        <%= f.text_field :name %>
      </p>
      <p>
        <%= f.label :description %><br />
        <%= f.text_area :description, :rows => 4 %>
        </p>
         <p>
        <%= f.label :ingredient_names, "Ingredients" %><br />
        <%= f.text_area :ingredient_names, :rows => 8 %>
      </p>
      <p>
        <%= f.label :step_instructions, "Instructions" %><br />
        <%= f.text_area :step_instructions, :rows => 8 %>
      </p>
      <p><%= f.submit %></p>
    <% end %>

My database schema:

    ActiveRecord::Schema.define(:version => 20110714095329) do
      create_table "ingredients", :force => true do |t|
        t.string   "name"
        t.datetime "created_at"
        t.datetime "updated_at"
      end
      create_table "recipes", :force => true do |t|
        t.string   "name"
        t.text     "description"
        t.datetime "created_at"
        t.datetime "updated_at"
      end
      create_table "stepingreds", :force => true do |t|
        t.integer  "recipe_id"
        t.integer  "step_id"
        t.integer  "ingredient_id"
        t.float    "amount"
        t.datetime "created_at"
        t.datetime "updated_at"
      end
      create_table "steps", :force => true do |t|
        t.integer  "recipe_id"
        t.integer  "number"
        t.text     "instruction"
        t.datetime "created_at"
        t.datetime "updated_at"
      end
    end

Please let me know if you have any suggestions or can recommend another piece of sample code I could model this app after.

  • 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-23T23:40:28+00:00Added an answer on May 23, 2026 at 11:40 pm

    You need to add accepts_nested_attributes_for :ingredients, :stepingreds, :steps in Recipe.rb to be able to create the associated objects through a single @recipe object.

    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've been following the rails edge guide in building a simple recipe app, and
Building my first SL MVVM application (Silverlight4 RC) and have some issues i don't
Building our Android app from Ant fails with this error: [apply] [apply] UNEXPECTED TOP-LEVEL
I am building a small Recipe Organiser MVC project with EntityFramework Code first and
I'm building a website and I have tested this sql statement on the production
I'm quite new to Buildout, but I just got my first Django application building
I'm building a recipe-finder for a new food blog. The design I have basically
Building on this question: Django Celery Time Limit Exceeded? I have some tasks that
Building a Shopping Cart app. Some products have options, some don't. I visitor can
I am building a recipe app where a user can view recipes, list ingredients,

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.