Archive for October, 2007

Rails : Accessing session data in models

Posted in activerecord, cache_sweeper, rubyonrails on October 25th, 2007 by Prateek Dayal – Be the first to comment

MVC architecture is good for keeping your design neat but once in a while you hit a road block and this is one such situtation. If you want to log actions/events in your code and track changes to models, you would want to do that in the activerecord callback hooks like after_save and before_destroy. However these methods have only access to the record that is affected and not to the session data. Therefore, you cannot log ‘who’ made the change.

After some googling around and wasting an hour or so, I found out that the trick is to use cache_sweeper class, which keeps a track of the objects that you ask it to for regenerating caches. You can however use it to keep a track of your changes and log it. Cache Sweeper objects also have access to the session data so you can know ‘who’ made changes to your model.

The process is described further in Rails Recipes book and is quite interesting … go read on :)

Popularity: 10% [?]

What is object oriented programming

Posted in Uncategorized on October 23rd, 2007 by Prateek Dayal – Be the first to comment

A talk on object oriented programming by Dan Ingallis, one of the people involved with smalltalk, one of the first OO language

Popularity: 8% [?]

ruby’s respond_to? for checking if a method exists

Posted in ruby, rubyonrails on October 16th, 2007 by Prateek Dayal – 4 Comments

Ruby has a function called respond_to? that can be used to seeing if a particular class or object has a method with a certain name. The syntax is something like


User.respond_to?('name') # returns true is method name exists otherwise false

This came in very handy the other day when I wanted to send emails in muziboo to people whenever they get a reply for either their posts, or a comment on their song or other stuff. Since I am using acts_as_commentable for all commenting, I added this to comments.rb (model)

after_create
if commentable.respond_to?('notify_comment')
commentable.notify_comment()
end
end

Now every model, for which I wanted to send an email (when a new comment is added), i defined notify_comment() function and called UserNotifier.deliver_xxx method inside that. For models where I did not want to send an email, I never defined the function and everything worked fine because respond_to? will return a false for those models

The above really helped me simplify the controller code and keep the code consistent. Thought of sharing this with everyone. Please do leave your comments if something is not quite right :)

Popularity: 53% [?]

Rubyworks on Debian Etch

Posted in debian, error, etch, muziboo, rmagick, ruby, rubyonrails, rubyworks on October 13th, 2007 by Prateek Dayal – 2 Comments

I recently got a new VPS account and it runs 64 bit Debain Etch (4.0). I installed rubyworks on it and the installation went pretty smooth. After that, I tried to move muziboo.com files to this server and a lot of error started happening in trying to get the app up.

First one was


muziboo@debian:~/muziboo/muz$ rake db:migrate
(in /home/muziboo/muziboo/muz)
rake aborted!
no such file to load -- hpricot

When I tried to install hpricot and that resulted in the following


muziboo@debian:~/muziboo/muz$ gem install hpricot
ERROR: While executing gem ... (RuntimeError)
Unsupported architecture: x86_64

To get rid of this error, I downloaded the latest rubygems package from rubyforge and the gem command started working fine. However I ran into this error now


debian:~/Softwares/rubygems-0.9.4# gem install hpricot --source http://code.whytheluckystiff.net
Bulk updating Gem source index for: http://code.whytheluckystiff.net
Select which gem to install for your platform (x86_64-linux)
1. hpricot 0.6 (mswin32)
2. hpricot 0.6 (jruby)
3. hpricot 0.6 (ruby)
4. hpricot 0.6 (jruby)
5. hpricot 0.6 (mswin32)
6. hpricot 0.6 (ruby)
7. Skip this gem
8. Cancel installation
> 3
Building native extensions. This could take a while...
ERROR: While executing gem ... (Gem::Installer::ExtensionBuildError)
ERROR: Failed to build gem native extension.

ruby extconf.rb install hpricot --source http://code.whytheluckystiff.net
extconf.rb:1:in `require': no such file to load -- mkmf (LoadError)
from extconf.rb:1

Gem files will remain installed in /usr/lib/ruby/gems/1.8/gems/hpricot-0.6 for inspection.
Results logged to /usr/lib/ruby/gems/1.8/gems/hpricot-0.6/ext/hpricot_scan/gem_make.out

I googled around a bit and then followed this post and the error disappeared but I started getting a new error about gcc missing on the system (during native extensions building stage). I installed that with apt-get install gcc-* (doing just apt-get install gcc resulted in header files missing error message). This time the native extensions were built properly and the gem was installed.

The next problem came with rmagick. I installed imagemagick and libmagick9-dev (to avoid getting a missing Magick-config error).

Once everything was working, I could run the app using mongrel_rails start but I could still not get it up in the rubyworks framework. I would always get a 500 error. I had symlinked /usr/rails to my app’s directory.

After lots of tests and experiments, I found out that rubyworks runs the processes as user rails and therefore your app’s directory should be writeable by user rails. I don’t know what is a good way to do that but I tried to make rails the owner of the app’s directory, the application started running all fine.

So these are some of the steps where i faced some problems and thought of writing about it. I hope someone will save some time because of this. If you do, please let me know :)

Popularity: 13% [?]