mercredi 28 février 2018

Create data with multiple nested attributes in rails

I am trying to create an item with nested attributes, these are my tables

1) Item

    has_many :items_modifier_groups, dependent: :destroy
    has_many :items_modifier_group_items, dependent: :destroy

    attr_accessible :items_modifier_groups_attributes, :items_modifier_group_items_attributes
    accepts_nested_attributes_for :items_modifier_groups, :items_modifier_group_items

2) ModifierGroup
    attr_accessible: tag_id

3) ItemsModifierGroup

    belongs_to :modifier_group
    belongs_to :item
    has_many :items_modifier_group_items, dependent: :destroy

    attr_accessible :item_id, :modifier_group_id, :modifier_group_attributes, :items_modifier_group_items_attributes
    accepts_nested_attributes_for :modifier_group, :items_modifier_group_items

4) ItemsModifierGroupItem - Table4

    belongs_to :items_modifier_group
    belongs_to :item

    attr_accessible :items_modifier_group_id, :item_id, :pre_select

Parameter to create from console

c = Item.new("name" => "test1", "items_modifier_groups_attributes"=>{"0"=>{"items_modifier_group_items_attributes"=>{"0"=>{"pre_select"=>"true"}},"min_item"=>"1", "modifier_group_attributes"=>{ "tag_id"=>"5" }}})

c.save

So when it create one item the item_id is nil in ItemsModifierGroupItem table but item_id is created in ItemsModifierGroup table, am i missing anything in the params?

Aucun commentaire:

Enregistrer un commentaire