joeunrue wrote:After some experimenting I found out that everything was working exactly as it was supposed to, only in the wrong order. acts_as_tree obviously has its own before_destroy method and was taking precedence over my method so it was starting with the children and working its way back up. Since none of those children had any children to save it just deleted them like it should.
argh, that's a weird one. Happy to help, this forum was a major lifeline for me when i was doing my first RoR project last year so it's karma payback
Another useful debugging method is the logger: you can type 'logger.debug "some text"' anywhere and the result will be output to log/development.log, which you can then view with tail or a dynamic log viewer if you have one. I have this method in config/environment.rb which automatically prefixes the location of the logger call and also prefixes it with "###" so it stands out:
#in config/environment.rb
def ldb(debug_text)
logger.debug "### #{caller[0]}: #{debug_text}"
end
Now you can type 'ldb "some text"' anywhere. For example, i have the following controller action:
def edit_schools
@schools = School.find(:all, :include => [:music_service], :order => "music_services.name, schools.name")
#put instrumental subscriber schools ids into an array, all prefixed by s, eg ["s1", "s3", etc]
@instrumental_school_ids = School.find_all_by_instrumental_subscriber(true).collect{|s| "s#{s.id}"}
ldb "@chools.size = #{@schools.size}, @instrumental_school_ids.size = #{@instrument_school_ids.size}"
end
which would output this to log/development.log when i access that controller (besides my ldb call, rails automatically outputs lots of stuff to the logger, such as controller calls with params (very useful for diagnosis!) and any sql calls to the db). You can see my ldb result down the bottom.
Processing SchoolsController#edit_schools (for 127.0.0.1 at 2008-05-29 15:13:28) [GET]
Session ID: b0ab94682560807c1b17968006b30530
Parameters: {"action"=>"edit_schools", "controller"=>"admin/schools"}
School Load Including Associations (0.001154) SELECT `schools`.`id` AS t0_r0, `schools`.`name` AS t0_r1, `schools`.`music_service_id` AS t0_r2, `schools`.`instrumental_subscriber` AS t0_r3, `music_services`.`id` AS t1_r0, `music_services`.`created_at` AS t1_r1, `music_services`.`name` AS t1_r2, `music_services`.`host` AS t1_r3, `music_services`.`is_live` AS t1_r4, `music_services`.`html_header` AS t1_r5, `music_services`.`denies_comments` AS t1_r6, `music_services`.`message` AS t1_r7, `music_services`.`locality` AS t1_r8, `music_services`.`signup_quote` AS t1_r9, `music_services`.`signup_quote_attribution` AS t1_r10, `music_services`.`signup_email` AS t1_r11, `music_services`.`signup_phone` AS t1_r12, `music_services`.`signup_contact_name` AS t1_r13, `music_services`.`small_logo_file` AS t1_r14, `music_services`.`medium_logo_file` AS t1_r15 FROM `schools` LEFT OUTER JOIN `music_services` ON `music_services`.id = `schools`.music_service_id ORDER BY music_services.name, schools.name
School Load (0.000321) SELECT * FROM `schools` WHERE (`schools`.`instrumental_subscriber` = 1)
### /home/jars/rails/lesson_planner/branches/bundles/app/controllers/admin/schools_controller.rb:16:in `edit_schools': @chools.size = 251, @instrumental_school_ids.size = 247
###########################################
#If i've helped you then please recommend me at Working With Rails:
#
http://www.workingwithrails.com/person/
i-williams