tag:blogger.com,1999:blog-1315195499098314054.post4544756932098750774..comments2023-06-01T15:31:01.643+00:00Comments on Dale's Blog: Rails: Don't override initialize on ActiveRecord objectsDalehttp://www.blogger.com/profile/11738036806389110658noreply@blogger.comBlogger20125tag:blogger.com,1999:blog-1315195499098314054.post-26898256850353735172013-05-17T14:55:57.334+00:002013-05-17T14:55:57.334+00:00As "Blog-Name" stated, this is also the ...As "Blog-Name" stated, this is also the case for Rails 3.1Anonymoushttps://www.blogger.com/profile/05510711844555756852noreply@blogger.comtag:blogger.com,1999:blog-1315195499098314054.post-75491474468176526432012-09-07T12:43:51.245+00:002012-09-07T12:43:51.245+00:00Seems in Rails 3.2 the syntax has changed:
class ...Seems in Rails 3.2 the syntax has changed:<br /><br />class User < ActiveRecord::Base<br /> after_initialize do<br /> @my_cache = {}<br /> end<br />end<br /><br />worked for meTom Hutterhttps://www.blogger.com/profile/02511090476401646023noreply@blogger.comtag:blogger.com,1999:blog-1315195499098314054.post-76632818002169831602012-02-04T22:51:32.326+00:002012-02-04T22:51:32.326+00:00@Unknown Thanks for your comment, I've not loo...@Unknown Thanks for your comment, I've not looked at the code for a while but the issue I had was that initialize wasn't called at all when active record loaded the object back from the DB.Dalehttps://www.blogger.com/profile/11738036806389110658noreply@blogger.comtag:blogger.com,1999:blog-1315195499098314054.post-25460042453589850052012-02-02T21:52:49.622+00:002012-02-02T21:52:49.622+00:00This is nice, but you still might need to overwrit...This is nice, but you still might need to overwrite ActiveRecord's initialize function. I think your error is due to the fact that the initializer takes a Hash as an argument, which explains you're "You have a nil object when you didn't expect it!". The proper call to override it would be this if I'm not mistaken:<br /><br /> class User < ActiveRecord::Base<br /> def initialize(args = {})<br /> super<br /> @my_cache = {}<br /> end<br /> end<br /><br />Then you'll see those error messages go away.Unknownhttps://www.blogger.com/profile/14676409230218925674noreply@blogger.comtag:blogger.com,1999:blog-1315195499098314054.post-88140183606706481962011-10-22T09:21:08.653+00:002011-10-22T09:21:08.653+00:00Excellent. That's just what was confusing me, ...Excellent. That's just what was confusing me, thanks.Robhttps://www.blogger.com/profile/03895194991837017656noreply@blogger.comtag:blogger.com,1999:blog-1315195499098314054.post-86577470313612037312011-08-30T20:21:23.291+00:002011-08-30T20:21:23.291+00:00Thanks -- exactly what I wanted to do and exactly ...Thanks -- exactly what I wanted to do and exactly the problem I ran into.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-1315195499098314054.post-72448115691082973642010-11-16T08:41:40.305+00:002010-11-16T08:41:40.305+00:00You have saved me a day. Thanks.You have saved me a day. Thanks.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-1315195499098314054.post-77888344516180747692010-11-07T17:50:03.407+00:002010-11-07T17:50:03.407+00:00thnaks! thats w0rk3d for me, but
DEPRECATION WARNI...thnaks! thats w0rk3d for me, but<br />DEPRECATION WARNING: Base#after_initialize has been deprecated, please use Base.after_initialize :method instead.lesslesshttps://www.blogger.com/profile/05104226702863820565noreply@blogger.comtag:blogger.com,1999:blog-1315195499098314054.post-3053471361611334232010-05-20T16:51:44.395+00:002010-05-20T16:51:44.395+00:00Thanks, that really helps. I mean, I actually need...Thanks, that really helps. I mean, I actually needed to do what you said not to do, but the code helped :)Dan Rosenstarkhttps://www.blogger.com/profile/12477764482153529031noreply@blogger.comtag:blogger.com,1999:blog-1315195499098314054.post-41883535411992446942010-01-10T08:11:30.382+00:002010-01-10T08:11:30.382+00:00I've found that I need to use both.
I have an...I've found that I need to use both.<br /><br />I have an AR object that handles a few different files. I got sick of defining setter methods to handle these files so I created a method to automatically generate the setters using class_eval. Unfortunately, I have to call the method twice. Once in initalize so it works for new objects and once again in after_initalize so it works for updates. In after_initalize I also have to check that the file setter methods haven't been created already so that's additional overhead.<br /><br />It all works but I worry that the code is much less clear than I originally intended.Steven Cummingshttps://www.blogger.com/profile/15840077223281952670noreply@blogger.comtag:blogger.com,1999:blog-1315195499098314054.post-74446700401346757612009-12-09T05:06:10.299+00:002009-12-09T05:06:10.299+00:00Thanks for the great description. This saved me a ...Thanks for the great description. This saved me a ton of time.Stephenhttps://www.blogger.com/profile/01356803756509162192noreply@blogger.comtag:blogger.com,1999:blog-1315195499098314054.post-92106609082419559572009-11-29T23:07:02.110+00:002009-11-29T23:07:02.110+00:00You saved the day (looked 4 hours for the error .....You saved the day (looked 4 hours for the error ..)Oliver Haaghttps://www.blogger.com/profile/14135480564589893458noreply@blogger.comtag:blogger.com,1999:blog-1315195499098314054.post-24870566784704160762009-11-13T16:13:17.889+00:002009-11-13T16:13:17.889+00:00Thanks a lot!!!!!!Thanks a lot!!!!!!Take -IT- Easyhttps://www.blogger.com/profile/17323811196740756669noreply@blogger.comtag:blogger.com,1999:blog-1315195499098314054.post-50186550204205279472009-10-27T08:13:09.061+00:002009-10-27T08:13:09.061+00:00@SoccerShoutPhil 1) Not sure, I've not done an...@SoccerShoutPhil 1) Not sure, I've not done any Rails for a while. 2) I used this method for calculated values that would not be stored in the database.Dalehttps://www.blogger.com/profile/11738036806389110658noreply@blogger.comtag:blogger.com,1999:blog-1315195499098314054.post-7987539344108178352009-10-01T15:49:33.035+00:002009-10-01T15:49:33.035+00:00Thanks - That's just fixed a very long standin...Thanks - That's just fixed a very long standing bug of mine - I was using "before_create" but it wasn't getting called early enough.. xxxIan Moodyhttps://www.blogger.com/profile/07282282708983598888noreply@blogger.comtag:blogger.com,1999:blog-1315195499098314054.post-60604813812938770272009-07-01T01:47:30.900+00:002009-07-01T01:47:30.900+00:00Thanks for this post.
Two things:
1) I had to u...Thanks for this post.<br /><br />Two things:<br /><br />1) I had to use 'self.myattribute' instead of '@myattribute' - not sure why (I'm pretty new)<br /><br />2) Remember to check whether 'myattribute' is null, otherwise you'll overwrite what is in your db when you recall the object.SoccerShoutPhilhttps://www.blogger.com/profile/08047494493679434922noreply@blogger.comtag:blogger.com,1999:blog-1315195499098314054.post-65128581742411749452009-06-01T19:11:31.682+00:002009-06-01T19:11:31.682+00:00Thank you!! One year old post still benefiting Rai...Thank you!! One year old post still benefiting Rails users :)Abhijit Dixithttps://www.blogger.com/profile/02679743852690373780noreply@blogger.comtag:blogger.com,1999:blog-1315195499098314054.post-40203855818255920622009-04-08T09:16:00.000+00:002009-04-08T09:16:00.000+00:00This comment has been removed by the author.Livet är en kod.https://www.blogger.com/profile/02696783299596181456noreply@blogger.comtag:blogger.com,1999:blog-1315195499098314054.post-6378568935755257472008-08-28T01:00:00.000+00:002008-08-28T01:00:00.000+00:00Thank you Thank you Thank youThank you Thank you Thank youSxadvihttps://www.blogger.com/profile/02495427492823642269noreply@blogger.comtag:blogger.com,1999:blog-1315195499098314054.post-72104041426257711462008-05-22T08:24:00.000+00:002008-05-22T08:24:00.000+00:00Thanks - this information was just what I needed :...Thanks - this information was just what I needed :)Casper Fabriciushttps://www.blogger.com/profile/10677028529604524610noreply@blogger.com