serialization - How to serialize nested Model in Rails? -



serialization - How to serialize nested Model in Rails? -

i'm having extremely hard time figuring out how serialize nested attributes of model in rails. have recipetemplate store existing recipe in it's template_data attribute. recipe has nested attributes 2 levels deep.

this on rails 3.1.0.rc4

class recipetemplate < activerecord::base serialize :template_data, recipe ... end class recipe < activerecord::base has_many :ingredients accepts_nested_attributes_for :ingredients ... end

ingredients in recipe has nested attributes (subingredients).

if set template_data object so:

recipe.includes(:ingredients => [:sub_ingredients]).find(1)

i'll typeerror "can't dump anonymous class class" makes sense, since doesn't know how serialize ingredients or subingredients.

how can serialize nested attributes in model can use:

serialize :template_data, recipe

or have serialize info in other manner , perform type safety checks myself?

thanks in advance help

i can see why want template stored within of serialized column, need little more manipulation of info beingness stored permitted type of column. here's do:

app/models/recipe_template.rb

class recipetemplate < activerecord::base serialize :template_data attr_accessible :name, :recipe def recipe=(r) self.template_data = r.serializable_hash_for_template end def recipe recipe.new(template_data) end end

app/models/recipe.rb

class recipe < activerecord::base has_many :ingredients, as: :parent accepts_nested_attributes_for :ingredients attr_accessible :name, :ingredients_attributes def serializable_hash_for_template(options={}) options[:except] ||= [:id, :created_at, :updated_at] serializable_hash(options).tap |h| h[:ingredients_attributes] = ingredients.map(&:serializable_hash_for_template) end end end

app/models/ingredient.rb

class ingredient < activerecord::base belongs_to :parent, polymorphic: true has_many :sub_ingredients, class_name: 'ingredient', as: :parent accepts_nested_attributes_for :sub_ingredients attr_accessible :name, :sub_ingredients_attributes def serializable_hash_for_template(options={}) options[:except] ||= [:id, :parent_id, :parent_type, :created_at, :updated_at] serializable_hash(options).tap |h| h[:sub_ingredients_attributes] = sub_ingredients.map(&:serializable_hash_for_template) end end end

then create , utilize template:

# create recipe utilize template taco_meat = ingredient.create(name: "taco meat") taco_seasoning = taco_meat.sub_ingredients.create(name: "taco seasoning") sams_tacos = recipe.create(name: "sam's tacos") sams_tacos.ingredients << taco_meat # create template recipe taco_recipe = recipetemplate.create(name: "taco recipe", recipe: sams_tacos) # build new recipe template another_taco_recipe = taco_recipe.recipe

the difference you're using serialized column store hash utilize in recipe contructor. if wanted serialize object, other posters correct–just associate object.

ruby-on-rails serialization activerecord nested-attributes

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -