I’m developing Ruby 1.8.7 and Rails 2.3.8.
I’m trying to create same id another model in an action.
def create
id = params[:id]
item_master = ItemMaster.new(params[:item_master)
item_master.id = id
item_master.save
item_master_child = ItemMaster.new(params[:item_master_child])
item_master_child.id = id
item_master_child.save
p item_master
# => ItemMaster id: 654, style: 6, icon: 7
p item_master_child
# => ItemMasterChild id: 654, sub_style: 4, roll: 5
end
It seems that save finished successfully, but actually item_master params becomes id: 654, style: 0, icon :0. All params except id becomes 0.
Any ideas?
The probrem is can’t mass-assign ID attriburtes, since it is protected attributes.
I’ve solved this way.
Make new ItemMaster and set only id attribute and save.
Make new ItemMasterChild with post params and set id attibute and save.
Then, update ItemMaster attibutes by post atttributes,this works fine.
Updating attibutes should be after save item_master_child.
If put it after item_master.save, the attributes are set as 0.
I don’t know why….
Thanks for all your support and replies.