<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Rails Forum - Ruby on Rails Help and Discussion Forum - Frozen Hash error with before_destroy]]></title>
	<link rel="self" href="http://railsforum.com/extern.php?action=feed&amp;tid=18685&amp;type=atom"/>
	<updated>2008-05-29T14:18:28Z</updated>
	<generator>PunBB</generator>
	<id>http://railsforum.com/viewtopic.php?id=18685</id>
		<entry>
			<title type="html"><![CDATA[Re: Frozen Hash error with before_destroy]]></title>
			<link rel="alternate" href="http://railsforum.com/viewtopic.php?pid=63509#p63509"/>
			<content type="html"><![CDATA[<div class="quotebox"><cite>joeunrue wrote:</cite><blockquote><p>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.</p></blockquote></div><p>argh, that&#039;s a weird one.&nbsp; Happy to help, this forum was a major lifeline for me when i was doing my first RoR project last year so it&#039;s karma payback <img src="http://railsforum.com/img/smilies/smile.png" width="15" height="15" alt="smile" /> </p><p>Another useful debugging method is the logger:&nbsp; you can type &#039;logger.debug &quot;some text&quot;&#039; 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.&nbsp; I have this method in config/environment.rb which automatically prefixes the location of the logger call and also prefixes it with &quot;###&quot; so it stands out:</p><p><pre name="code" class="ruby:nogutter">#in config/environment.rb<br />def ldb(debug_text)<br />&nbsp; logger.debug &quot;### #{caller[0]}: #{debug_text}&quot;<br />end</pre><br />Now you can type &#039;ldb &quot;some text&quot;&#039; anywhere.&nbsp; For example, i have the following controller action:<br /><pre name="code" class="ruby:nogutter">&nbsp; def edit_schools<br />&nbsp; &nbsp; @schools = School.find(:all, :include =&gt; [:music_service], :order =&gt; &quot;music_services.name, schools.name&quot;)<br />&nbsp; &nbsp; #put instrumental subscriber schools ids into an array, all prefixed by s, eg [&quot;s1&quot;, &quot;s3&quot;, etc]<br />&nbsp; &nbsp; @instrumental_school_ids = School.find_all_by_instrumental_subscriber(true).collect{|s| &quot;s#{s.id}&quot;}<br />&nbsp; &nbsp; ldb &quot;@chools.size = #{@schools.size}, @instrumental_school_ids.size = #{@instrument_school_ids.size}&quot;<br />&nbsp; end</pre><br />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).&nbsp; You can see my ldb result down the bottom.</p><p><pre name="code" class="ruby:nogutter">Processing SchoolsController#edit_schools (for 127.0.0.1 at 2008-05-29 15:13:28) [GET]<br />&nbsp; Session ID: b0ab94682560807c1b17968006b30530<br />&nbsp; Parameters: {&quot;action&quot;=&gt;&quot;edit_schools&quot;, &quot;controller&quot;=&gt;&quot;admin/schools&quot;}<br />&nbsp; School Load Including Associations (0.001154)&nbsp; &nbsp;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<br />&nbsp; School Load (0.000321)&nbsp; &nbsp;SELECT * FROM `schools` WHERE (`schools`.`instrumental_subscriber` = 1) <br />### /home/jars/rails/lesson_planner/branches/bundles/app/controllers/admin/schools_controller.rb:16:in `edit_schools&#039;: @chools.size = 251, @instrumental_school_ids.size = 247</pre></p>]]></content>
			<author>
				<name><![CDATA[Max Williams]]></name>
				<uri>http://railsforum.com/profile.php?id=4686</uri>
			</author>
			<updated>2008-05-29T14:18:28Z</updated>
			<id>http://railsforum.com/viewtopic.php?pid=63509#p63509</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Frozen Hash error with before_destroy]]></title>
			<link rel="alternate" href="http://railsforum.com/viewtopic.php?pid=63405#p63405"/>
			<content type="html"><![CDATA[<p>God I hate it when I finally figure things out and feel so stupid afterwards.</p><p>I&#039;m a total amateur when it comes to debugging RoR applications and forgot I could puts things to the console. 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.</p><p>All I had to do to fix this thing was move my before_destroy method above the acts_as_tree declaration. Fixed.</p><br /><br /><p>Thank you so much for your help, Max.</p>]]></content>
			<author>
				<name><![CDATA[joeunrue]]></name>
				<uri>http://railsforum.com/profile.php?id=4773</uri>
			</author>
			<updated>2008-05-28T16:55:10Z</updated>
			<id>http://railsforum.com/viewtopic.php?pid=63405#p63405</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Frozen Hash error with before_destroy]]></title>
			<link rel="alternate" href="http://railsforum.com/viewtopic.php?pid=63402#p63402"/>
			<content type="html"><![CDATA[<div class="quotebox"><cite>joeunrue wrote:</cite><blockquote><p>The second solution, at least, works when I enter it by itself into the console, and neither of them throw up any frozen hash errors, but&nbsp; neither of them actually work when used with the destroy method. It&#039;s not even updating the database. It just destroys both pages.</p></blockquote></div><p>Weird.&nbsp; what happens if you comment out all the code in save_children and just have <br />&nbsp; puts &quot;hello!&quot;</p><p>instead - do you see that output in the console when you destroy the page?</p>]]></content>
			<author>
				<name><![CDATA[Max Williams]]></name>
				<uri>http://railsforum.com/profile.php?id=4686</uri>
			</author>
			<updated>2008-05-28T16:32:38Z</updated>
			<id>http://railsforum.com/viewtopic.php?pid=63402#p63402</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Frozen Hash error with before_destroy]]></title>
			<link rel="alternate" href="http://railsforum.com/viewtopic.php?pid=63397#p63397"/>
			<content type="html"><![CDATA[<p>The second solution, at least, works when I enter it by itself into the console, and neither of them throw up any frozen hash errors, but&nbsp; neither of them actually work when used with the destroy method. It&#039;s not even updating the database. It just destroys both pages.</p>]]></content>
			<author>
				<name><![CDATA[joeunrue]]></name>
				<uri>http://railsforum.com/profile.php?id=4773</uri>
			</author>
			<updated>2008-05-28T15:47:14Z</updated>
			<id>http://railsforum.com/viewtopic.php?pid=63397#p63397</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Frozen Hash error with before_destroy]]></title>
			<link rel="alternate" href="http://railsforum.com/viewtopic.php?pid=63396#p63396"/>
			<content type="html"><![CDATA[<p>Actually, thinking about it more, i think it was the references to self.children that were breaking it.&nbsp; If my last suggestion doesn&#039;t work, try</p><p>&nbsp; def save_children<br />&nbsp; &nbsp; Page.find_all_by_parent_id(self.id).each do |c|<br />&nbsp; &nbsp; &nbsp; c.update_attributes(:parent_id =&gt; self.parent_id)<br />&nbsp; &nbsp; end<br />&nbsp; end</p><p>I think the frozen hash problem has to do with accessing associations while/just before deleting: eg if a post has many comments, then &#039;post.comments&#039; might cause the frozen hash error while &#039;Comment.find_all_by_post_id(post.id)&#039; would be ok.</p>]]></content>
			<author>
				<name><![CDATA[Max Williams]]></name>
				<uri>http://railsforum.com/profile.php?id=4686</uri>
			</author>
			<updated>2008-05-28T15:10:28Z</updated>
			<id>http://railsforum.com/viewtopic.php?pid=63396#p63396</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Frozen Hash error with before_destroy]]></title>
			<link rel="alternate" href="http://railsforum.com/viewtopic.php?pid=63394#p63394"/>
			<content type="html"><![CDATA[<div class="quotebox"><cite>joeunrue wrote:</cite><blockquote><p>Thanks for the reply, Max. I&#039;ve got a model &quot;Page&quot; that acts_as_tree that I&#039;m using for my content management system. I&#039;m trying to let the administrators delete a page from the tree and not lose all the children.</p><p>class Page &lt; ActiveRecord::Base<br />&nbsp; acts_as_tree :order =&gt; :title<br />&nbsp; before_destroy :save_children<br />&nbsp; <br />&nbsp; def save_children<br />&nbsp; &nbsp; for c in self.children do<br />&nbsp; &nbsp; &nbsp; c.parent_id = self.parent_id<br />&nbsp; &nbsp; &nbsp; c.save<br />&nbsp; &nbsp; end<br />&nbsp; end<br />end</p><p>There&#039;s the important parts of the Page model. When I run it in the console by itself it works just fine. However in conjuction with the destroy method it gives me the frozen hash error.</p></blockquote></div><p>i wish i could remember what my problem was. <img src="http://railsforum.com/img/smilies/sad.png" width="15" height="15" alt="sad" />&nbsp; It was something equally random.&nbsp; I think i worked around it by not using the usual AR save.&nbsp; Try this:</p><p>&nbsp; def save_children<br />&nbsp; &nbsp; ActiveRecord::Base.connection().execute(&quot;update pages set parent_id = #{self.parent_id} where parent_id = #{self.id}&quot;)<br />&nbsp; end</p><p>this is a total guess...</p>]]></content>
			<author>
				<name><![CDATA[Max Williams]]></name>
				<uri>http://railsforum.com/profile.php?id=4686</uri>
			</author>
			<updated>2008-05-28T15:00:46Z</updated>
			<id>http://railsforum.com/viewtopic.php?pid=63394#p63394</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Frozen Hash error with before_destroy]]></title>
			<link rel="alternate" href="http://railsforum.com/viewtopic.php?pid=63392#p63392"/>
			<content type="html"><![CDATA[<p>Thanks for the reply, Max. I&#039;ve got a model &quot;Page&quot; that acts_as_tree that I&#039;m using for my content management system. I&#039;m trying to let the administrators delete a page from the tree and not lose all the children.</p><p>class Page &lt; ActiveRecord::Base<br />&nbsp; acts_as_tree :order =&gt; :title<br />&nbsp; before_destroy :save_children<br />&nbsp; <br />&nbsp; def save_children<br />&nbsp; &nbsp; for c in self.children do<br />&nbsp; &nbsp; &nbsp; c.parent_id = self.parent_id<br />&nbsp; &nbsp; &nbsp; c.save<br />&nbsp; &nbsp; end<br />&nbsp; end<br />end</p><p>There&#039;s the important parts of the Page model. When I run it in the console by itself it works just fine. However in conjuction with the destroy method it gives me the frozen hash error.</p>]]></content>
			<author>
				<name><![CDATA[joeunrue]]></name>
				<uri>http://railsforum.com/profile.php?id=4773</uri>
			</author>
			<updated>2008-05-28T14:16:39Z</updated>
			<id>http://railsforum.com/viewtopic.php?pid=63392#p63392</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Frozen Hash error with before_destroy]]></title>
			<link rel="alternate" href="http://railsforum.com/viewtopic.php?pid=63370#p63370"/>
			<content type="html"><![CDATA[<div class="quotebox"><cite>joeunrue wrote:</cite><blockquote><p>I have a method that works just fine when run by itself, but in conjunction with &quot;before_destroy&quot; it craps out.</p><p>Essentially I have a acts_as_tree model and am trying to preserve the children when I delete a node on the tree. My method &quot;save_children&quot; turns the children&#039;s parent into the parent of the soon-to-be-deleted object. When I try to use it I keep getting a &quot;can&#039;t modify frozen hash&quot; error and I haven&#039;t been able to find a solution that will work. Is this even the best way to try and &quot;bubble up&quot; children on a tree? Should I even be using a tree? I&#039;m at a total loss.</p><p>Saxon</p></blockquote></div><p>I had that &#039;can&#039;t modify frozen hash&#039; thing and it turned out to be a mistake in my code (can&#039;t remember any more details, sorry).&nbsp; Will you post your method up?</p><p>As to whether you should be using acts_as_tree - i dunno, what are you trying to do?</p>]]></content>
			<author>
				<name><![CDATA[Max Williams]]></name>
				<uri>http://railsforum.com/profile.php?id=4686</uri>
			</author>
			<updated>2008-05-28T09:03:12Z</updated>
			<id>http://railsforum.com/viewtopic.php?pid=63370#p63370</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Frozen Hash error with before_destroy]]></title>
			<link rel="alternate" href="http://railsforum.com/viewtopic.php?pid=63317#p63317"/>
			<content type="html"><![CDATA[<p>I have a method that works just fine when run by itself, but in conjunction with &quot;before_destroy&quot; it craps out.</p><p>Essentially I have a acts_as_tree model and am trying to preserve the children when I delete a node on the tree. My method &quot;save_children&quot; turns the children&#039;s parent into the parent of the soon-to-be-deleted object. When I try to use it I keep getting a &quot;can&#039;t modify frozen hash&quot; error and I haven&#039;t been able to find a solution that will work. Is this even the best way to try and &quot;bubble up&quot; children on a tree? Should I even be using a tree? I&#039;m at a total loss.</p><p>Saxon</p>]]></content>
			<author>
				<name><![CDATA[joeunrue]]></name>
				<uri>http://railsforum.com/profile.php?id=4773</uri>
			</author>
			<updated>2008-05-27T19:48:55Z</updated>
			<id>http://railsforum.com/viewtopic.php?pid=63317#p63317</id>
		</entry>
</feed>
