I''m attempting to make a basic CRUD/data entry cookbook. I''ve got a recipe model that is composed of other models. I''m intending of using a single form to create the recipes such that n ingredients and n instructions can be associated to a single recipe. I understand why it''s not happening I just don''t know how to make it stop. This is what I have so far as far as models and schema.rb: class Direction < ActiveRecord::Base belongs_to :recipe validates :number, :presence => true validates :instruction, :presence => true def step "#{number}.) #{instruction}" end end class Ingredient < ActiveRecord::Base has_one :measurement belongs_to :recipe validates :measurement, :presence => true validates :quantity, :presence => true validates :food, :presence => true end class Measurement < ActiveRecord::Base belongs_to :ingredient end class Recipe < ActiveRecord::Base has_many :ingredients has_many :directions validates :title, :presence => true validates :description, :presence => true accepts_nested_attributes_for :ingredients, :reject_if => proc { |a| a[:food].blank? } #accepts_nested_attributes_for :ingredients, :reject_if => proc { |a| a[:food].blank? } #we want to resuse ingredients if possible accepts_nested_attributes_for :directions #accepts_nested_attributes_for :directions, :reject_if => proc { |a| a[:instruction].blank? }, :allow_destroy => true #there should not be an issue with deleting directions associated to a deleted recipe #http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Method_Calls#Procs end # This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # # Note that this schema.rb definition is the authoritative source for your # database schema. If you need to create the application database on another # system, you should be using db:schema:load, not running all the migrations # from scratch. The latter is a flawed and unsustainable approach (the more migrations # you''ll amass, the slower it''ll run and the greater likelihood for issues). # # It''s strongly recommended to check this file into your version control system. ActiveRecord::Schema.define(:version => 20110314191853) do create_table "directions", :force => true do |t| t.integer "recipe_id" t.integer "number" t.text "instruction" t.datetime "created_at" t.datetime "updated_at" end create_table "ingredients", :force => true do |t| t.integer "measurement_id" t.string "quantity" t.string "food" t.datetime "created_at" t.datetime "updated_at" end create_table "measurements", :force => true do |t| t.string "size" t.float "quantity" t.datetime "created_at" t.datetime "updated_at" t.string "abbreviation" t.string "measurement" t.string "measurement_type" t.string "equivalent" end create_table "recipes", :force => true do |t| t.integer "ingredient_id" t.integer "direction_id" t.string "title" t.text "description" t.datetime "created_at" t.datetime "updated_at" t.string "source" t.string "image" t.string "cooktime" end create_table "sessions", :force => true do |t| t.string "session_id", :null => false t.text "data" t.datetime "created_at" t.datetime "updated_at" end add_index "sessions", ["session_id"], :name => "index_sessions_on_session_id" add_index "sessions", ["updated_at"], :name => "index_sessions_on_updated_at" end -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Mar 17, 2:57 pm, "R. K." <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I''m attempting to make a basic CRUD/data entry cookbook. I''ve got a > recipe model that is composed of other models. I''m intending of using a > single form to create the recipes such that n ingredients and n > instructions can be associated to a single recipe. > > I understand why it''s not happening I just don''t know how to make it > stop. >So what''s not working? The models you posted should be ok as long as the forms are ok too (there''s also a railscast or 2 on this subject) Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
R. K. wrote in post #987926:> I''m attempting to make a basic CRUD/data entry cookbook. I''ve got a > recipe model that is composed of other models. I''m intending of using a > single form to create the recipes such that n ingredients and n > instructions can be associated to a single recipe. > > I understand why it''s not happening I just don''t know how to make it > stop. > > This is what I have so far as far as models and schema.rb: > > class Direction < ActiveRecord::Base > belongs_to :recipe > > validates :number, :presence => true > validates :instruction, :presence => true > > def step > "#{number}.) #{instruction}" > end > end > > class Ingredient < ActiveRecord::Base > has_one :measurement > belongs_to :recipe > validates :measurement, :presence => true > validates :quantity, :presence => true > validates :food, :presence => true > end > > class Measurement < ActiveRecord::Base > belongs_to :ingredient > end > > class Recipe < ActiveRecord::Base > has_many :ingredients > has_many :directions > > validates :title, :presence => true > validates :description, :presence => true > > accepts_nested_attributes_for :ingredients, :reject_if => proc { |a| > a[:food].blank? } > #accepts_nested_attributes_for :ingredients, :reject_if => proc { |a| > a[:food].blank? } > #we want to resuse ingredients if possible > > accepts_nested_attributes_for :directions > #accepts_nested_attributes_for :directions, :reject_if => proc { |a| > a[:instruction].blank? }, :allow_destroy => true > #there should not be an issue with deleting directions associated to a > deleted recipe > > #http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Method_Calls#Procs > end > > # This file is auto-generated from the current state of the database. > Instead > # of editing this file, please use the migrations feature of Active > Record to > # incrementally modify your database, and then regenerate this schema > definition. > # > # Note that this schema.rb definition is the authoritative source for > your > # database schema. If you need to create the application database on > another > # system, you should be using db:schema:load, not running all the > migrations > # from scratch. The latter is a flawed and unsustainable approach (the > more migrations > # you''ll amass, the slower it''ll run and the greater likelihood for > issues). > # > # It''s strongly recommended to check this file into your version control > system. > > ActiveRecord::Schema.define(:version => 20110314191853) do > > create_table "directions", :force => true do |t| > t.integer "recipe_id" > t.integer "number" > t.text "instruction" > t.datetime "created_at" > t.datetime "updated_at" > end > > create_table "ingredients", :force => true do |t| > t.integer "measurement_id" > t.string "quantity" > t.string "food" > t.datetime "created_at" > t.datetime "updated_at" > end > > create_table "measurements", :force => true do |t| > t.string "size" > t.float "quantity" > t.datetime "created_at" > t.datetime "updated_at" > t.string "abbreviation" > t.string "measurement" > t.string "measurement_type" > t.string "equivalent" > end > > create_table "recipes", :force => true do |t| > t.integer "ingredient_id" > t.integer "direction_id" > t.string "title" > t.text "description" > t.datetime "created_at" > t.datetime "updated_at" > t.string "source" > t.string "image" > t.string "cooktime" > end > > create_table "sessions", :force => true do |t| > t.string "session_id", :null => false > t.text "data" > t.datetime "created_at" > t.datetime "updated_at" > end > > add_index "sessions", ["session_id"], :name => > "index_sessions_on_session_id" > add_index "sessions", ["updated_at"], :name => > "index_sessions_on_updated_at" > > endBasically, children need to know their parents. Take a look in the log at the queries generated, and you''ll see items like: Querying to show a recipe with id = 1: SELECT ''directions''.* FROM ''directions'' WHERE (''directions''.recipe_id = 1) So: Recipe (top of the food chain - haha) # table has no <model>_id fields has_many :ingredients has_many :directions Ingredient belongs_to :recipe # table must have recipe_id has_one :measurement Measurement belongs_to :ingredient # table must have ingredient_id Direction belongs_to :recipe # table must have recipe_id That should make your app happier. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung wrote in post #987930:> So what''s not working? The models you posted should be ok as long as > the forms are ok too (there''s also a railscast or 2 on this subject) > > FredYes Fred and examining my code some more (with the advice I got from here) that''s my conclusion as well. I am however getting a different error after tweaking the form a bit. In short I want to dynamically add my ingredients to my recipe so I can have N ingredients. I am using jquery to accomplish this i.e. default to one ingredient with a button to add the N that follow. Here is my current error: ActionController::RoutingError in Recipes#new Showing /Users/mkidd/Sites/cookbook/app/views/recipes/_form.html.erb where line #34 raised: No route matches {:action=>"new", :controller=>"ingredients", :format=>:js, :recipe_id=>#<Recipe id: nil, ingredient_id: nil, direction_id: nil, title: nil, description: nil, source: nil, image: nil, cooktime: nil, created_at: nil, updated_at: nil>} Extracted source (around line #34): 31: </fieldset> 32: <div id="ingredients"> 33: <h3>Ingredients</h3> 34: <%= link_to "Add Ingredient", new_recipe_ingredient_path(@recipe, :format => :js), :remote => true, :id => ''new_ingredient_link'' %> 35: </div> 36: <%# <p><%= link_to_add_ingredient "Add Ingredient", f, :ingredients #%><%#</p> #%> 37: <%# <p><%= link_to_add_fields "Add Ingredient", f, :ingredients #%><%#</p> #%> Looking at this error again I thought i had it by looking at my routes but I lost it. My routes don''t paste that well though. And to stop cutting and pasting here is my github for the current codebase: https://github.com/mkidd/cookbook -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.