Ruby on Rails Wednesday, February 28, 2018

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

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

ModifierGroup

  attr_accessible: tag_id

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

ItemsModifierGroupItem - Table4

  belongs_to :items_modifier_group    belongs_to :item      attr_accessible :items_modifier_group_id, :item_id, :pre_select

Params 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    INSERT INTO "items_modifier_group_items" ("created_at", "item_id", "items_modifier_group_id", "pre_select", "sequence_no", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["created_at", Wed, 28 Feb 2018 15:44:27 GST +04:00], ["item_id", nil], ["items_modifier_group_id", 35], ["pre_select", true], ["sequence_no", 0], ["updated_at", Wed, 28 Feb 2018 15:44:27 GST +04:00]


When it creates an item, the item_id is nil in ItemsModifierGroupItem table; however, item_id is created in ItemsModifierGroup table.

Am I missing anything in the params?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/77f50f74-b43e-4ea1-910c-2d97c0f94db5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment