Rails Kung-Fu
March 27th, 2008 tkadom Posted in Rails, Random |
Whenever I want to learn a new language, or framework to improve my kung-fu, I like to look at something that has been written in it. You generally get an idea of how much you will like a language just by looking at the syntax up close. With rails, I started with 1.2, and there were lots of examples out there which all seemed to just work. With the release of 2.0.2 having happened just recently, there are lots of rails 1.2 moves that will leave you in a face plant on the floor searching for a new master…
So how do you assimilate the new kung-fu? Well for me, I learn best by example. I like to see code, and I learn the most from good code. I had heard that lots of folks fell victim to setting up a beast forum. In truth, it was not that difficult as there were plenty of signs left by other seekers which could guide your path. If you paid really close attention, then you saw that beast is no longer beast. Beast has become… altered_beast ( and there is hardly a traveler that has traveled this path! )
This altered_beast is a wonderful master for students on the rails 2 path. For one thing, Rick Olson works on rails core. If you were to look for a more experienced teacher, you would be hard pressed to find one. For another thing, altered_beast grew from a forum that was supposed to not exceed 500 lines of code. So there are some pretty sweet moves in the code. And finally, altered_beast is a piece of cake to install. Seriously. If you can’t get altered_beast going, you will need to downgrade to a weenier framework than rails. And I got news for you, I don’t think there is a weenier one!
Just looking at the models in altered_beast is like a having someone hold your hand through how you model ActiveRecord relationships. For a rank beginner, you immediately see that you probably could be using the :dependend attribute on your has_many relationships. Looking through the user model you quickly realize what belongs in a model, and what belongs in a controller. You *may* also start looking for "concerned_with", and come up empty in rails documentation. And thats actually the first bit of altered_beast kung-fu that stumped me a little. At the top of the user model, altered beast presents "concerned_with". After a little digging I found the following definition in the initializer…
-
-
concerned_with(*concerns)
-
concerns.each do |concern|
-
require_dependency "#{name.underscore)/#{concern}"
-
end
-
end
-
end
-
So in rails 2 we run through these initializers when the app starts. and all concerned_with does is give you a nifty shortcut for requiring dependencies. However, If you have tried registering the user observer in altered_beast, this kung-fu will have chopped you in the knees already. It turns out there is a little issue with the order in which items get initialized in the 2.0.2 rails core, and this specific trick will punch you in the gut pretty nicely. I have a suspicion that it is what led to: Ticket 10980. You see when the user observer loads, it tries to instantiate a user. Users are concerned_with stuff, but at initialization time, concerned_with has not yet been defined because the observers are triggered first. So no matter how smart you try to get with defining your observer in the new rails 2 way, you will be out of luck.
A simple fix would be to update the user model and replace concerned_with with the require_dependencies. Another fix would be to freeze rails into altered_beast and monkey_patch it (which I recommend, because it feels a bit like your cheating, and it gets you into more of a rails mindset. If something is broken, don’t go looking for someone to fix it. just fix it!) There is plenty more rails kung-fu in altered_beast, and I think anyone who is looking for an introduction to rails, and who has some programming moves to begin with, could do much worse than to pull down the source and begin to assimilate it. When you can snatch the pebble from the altered_beast, The rails 1.2 moves will seem like child’s play…
June 20th, 2008 at 10:11 pm
Hi,
thanks for the article. I tried setting up altered beast but for some strange reason I can’t get past adding my user name and password. It just keeps going back to this screen. The logs don’t give any idea as to what the problem is. Any suggestions?
thanks,
Stjohn
March 25th, 2009 at 3:21 pm
Every time i come here I am not dissapointed, nice post
March 31st, 2010 at 4:43 am
Thank you for another great post. I look forward to many more entries with high quality info. I’m a marketer myself and your information always seems to get my business brain going!!
April 1st, 2010 at 8:48 pm
Outstanding post full of useful tips! My site is fairly new and I am having a hard time getting my subscribers to leave comments. They are coming to the site but I have the feeling that “nobody wants to be first”.
June 8th, 2010 at 4:31 pm
My sister and I really would like to thank you, this was good to read.