Skip to content

Instantly share code, notes, and snippets.

@freshtonic
Created February 14, 2010 10:52
Show Gist options
  • Save freshtonic/303952 to your computer and use it in GitHub Desktop.
Save freshtonic/303952 to your computer and use it in GitHub Desktop.
# Here are the params to my QuestionsController update action.
# accepts_nested_attributes_for is defined on the Question model
# for its child relationship Choice. (Question has many Choices).
# Adding new choices to a question in bulk as shown below works except for the order
# that the Choices are created in. Choices use acts_as_list and have a list_index column.
# The order that update_attributes (on the Question instance) creates
# the Choice objects is the sort order of the hash keys below. The 'new_NNNNN' id
# within the hash is created in JavaScript using new Date().getTime(), so it always
# increments. The order below just happens to be how the keys of the has are
# enumerated, (c a b) but the child choices where actually created ( a b c).
# Any idea how I can solve this? I don't really want to crack open
# update_attributes in order to sort keys lexically before creating child models.
{"commit"=>"Update",
"_method"=>"put",
"action"=>"update",
"id"=>"1",
"question"=>
{"text"=>
"What is your living situation? (married, co-habiting, living with parents, single, widowed etc)",
"choices_attributes"=>
{"new_1266143955074"=>{"text"=>"c", "id"=>"", "_destroy"=>""},
"new_1266143949291"=>{"text"=>"a", "id"=>"", "_destroy"=>""},
"new_1266143952987"=>{"text"=>"b", "id"=>"", "_destroy"=>""}},
"answer_type"=>"one_choice"},
"controller"=>"admin/questions"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment