You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I found unexpected behavior when I call insert_at(position) on a new item (as is shown in the test_list.rb in the gem). If I insert a newly created (and not yet saved object), increment_positions_on_lower_items gets called twice, so that given positions [1,2], new_object.insert_at(2) results in positions [1,2,4].
See the following:
## GET THE PARENTruby-1.9.2-p290:030 > shoot_list=ShootList.firstShootListLoad(0.6ms)SELECT"shoot_lists".* FROM"shoot_lists"LIMIT1=>#<ShootList id: 1, name: "xyx", manuscript_id: 1, lighting_config_id: nil, imaging_setup_id: nil, imaging_mode: nil, imaging_system_id: nil, created_at: "2011-11-11 22:26:39", updated_at: "2011-11-11 22:26:39"> ### TWO ORDERED CHILDREN IN THE LISTruby-1.9.2-p290:031 > shoot_list.shot_sequences.map(&:position)ShotSequenceLoad(0.7ms)SELECT"shot_sequences".* FROM"shot_sequences"WHERE"shot_sequences"."shoot_list_id"=1ORDERBYposition=>[1,2]## CREATE A NEW CHILD AND INSERT ITruby-1.9.2-p290:032 > shot_sequence=ShotSequence.new(:subject=>"elephant",:shoot_list=>shoot_list)=>#<ShotSequence id: nil, operator_instructions: nil, subject: "elephant", rotation_degrees: 0, position: nil, created_at: nil, updated_at: nil, shoot_list_id: 1> ruby-1.9.2-p290:033 > shot_sequence.insert_at(2)SQL(2.2ms)UPDATE"shot_sequences"SETposition=(position + 1)WHERE("shot_sequences"."shoot_list_id"=1ANDposition >= 2)(0.1ms) BEGIN
SQL(0.4ms)UPDATE"shot_sequences"SETposition=(position + 1)WHERE("shot_sequences"."shoot_list_id"=1ANDposition >= 2)SQL(0.7ms)INSERTINTO"shot_sequences"("created_at","operator_instructions","position","rotation_degrees","shoot_list_id","subject","updated_at")VALUES($1, $2, $3, $4, $5, $6, $7)RETURNING"id"[["created_at",Mon,14Nov201112:34:05EST -05:00],["operator_instructions",nil],["position",2],["rotation_degrees",0],["shoot_list_id",1],["subject","elephant"],["updated_at",Mon,14Nov201112:34:05EST -05:00]](0.4ms)COMMIT=> true### RELOAD THE PARENT AND LIST THE CHILDRENruby-1.9.2-p290:035 > shoot_list=ShootList.firstShootListLoad(0.6ms)SELECT"shoot_lists".* FROM"shoot_lists"LIMIT1=>#<ShootList id: 1, name: "xyx", manuscript_id: 1, lighting_config_id: nil, imaging_setup_id: nil, imaging_mode: nil, imaging_system_id: nil, created_at: "2011-11-11 22:26:39", updated_at: "2011-11-11 22:26:39"> ruby-1.9.2-p290:036 > shoot_list.shot_sequences.map(&:position)ShotSequenceLoad(0.8ms)SELECT"shot_sequences".* FROM"shot_sequences"WHERE"shot_sequences"."shoot_list_id"=1ORDERBYposition=>[1,2,4]ruby-1.9.2-p290:037 >
Note that two update calls are made to do the position increment. I stuck a backtrace printout in increment_positions_on_lower_items(position) and found the first call was made on insert and the second when the before_create callback is made.
Creating the item, and then inserting it in the correct position gets the desired result.
The text was updated successfully, but these errors were encountered:
Hmm.. this is indeed weird. I have a feeling this was introduced during one of the recent pull requests. I am looking into this now, thanks for reporting!
The test test_insert_middle_with_unsaved_item addresses this issue and I think its fixed in the latest version. Can you please verify that it works for you?
Note: The latest version is not pushed to Rubygems yet.
I found unexpected behavior when I call
insert_at(position)
on a new item (as is shown in thetest_list.rb
in the gem). If I insert a newly created (and not yet saved object),increment_positions_on_lower_items
gets called twice, so that given positions[1,2]
,new_object.insert_at(2)
results in positions[1,2,4]
.See the following:
Note that two update calls are made to do the position increment. I stuck a backtrace printout in
increment_positions_on_lower_items(position)
and found the first call was made on insert and the second when thebefore_create
callback is made.Creating the item, and then inserting it in the correct position gets the desired result.
The text was updated successfully, but these errors were encountered: