I am trying to get nested model forms . As far as I can tell, I'm doing everything right, but it still doesn't work.
I'm on Rails 3 beta 3.
My models as expected:
class Recipe < ActiveRecord::Base
has_many :ingredients, :dependent => :destroy
accepts_nested_attributes_for :ingredients
attr_accessible :name
end
class Ingredient < ActiveRecord::Base
attr_accessible :name, :sort_order, :amount
belongs_to :recipe
end
I can use Recipe.ingredients_attributes = as expected:
recipe = Recipe.new
recipe.ingredients_attributes = [ {:name=>"flour", :amount=>"1 cup"}, {:name=>"sugar", :amount=>"2 cups"}]
recipe.ingredients.size
However, I cannot create new object graphs using a hash of parameters as shown in the documentation :
params = { :name => "test", :ingredients_attributes => [ {:name=>"flour", :amount=>"1 cup"}, {:name=>"sugar", :amount=>"2 cups"}] }
recipe = Recipe.new(params)
recipe.name
recipe.ingredients
Am I doing something wrong here? Or is there a problem in the beta version of Rails 3?
Update
This is a mistake caused attr_accessible :namein the Recipe. This is not Rails3.
source
share