Posted by Andreas on Saturday, October 20, 2007 at 10:10 (CEST)
Trying to paginate special queries gave me a headache for quite some time. As summarized in one of my previous blog posts “Paginating special queries”, custom finder methods do not work without problems if you use the will_paginate plugin.
There are several plugins that make life easier when you need custom finder methods to do special queries in your models. Widely known are scope_out and scoped_proxy. Both of them are great plugins with different features, but unfortunately neither of them work out of the box with pagination.
Some days ago, Nick of Pivotal Blabs brought back to my mind, that there’s another plugin called HasFinder. HasFinder basically works like scope_out and scoped_proxy and combines advantages of both plugins. Besides the basic idea to define different scopes for an ActiveRecord model, you can also access methods (finder) in nested scopes that exactly behave like ActiveRecord associations do. Additionally, HasFinder works out of the box with will_paginate.
Head over to Nick’s HasFinder article to find out more about it. HasFinder is available as a gem or can be installed as a plugin from SVN. Great plugin, Nick.
Update 2008-04-02: HasFinder functionality will be included in Rails 2.1, called named_scope
Posted by Andreas on Friday, September 21, 2007 at 09:09 (CEST)
This morning I faced a strange problem when trying to write RSpec tests for a user login action. Trying to setting the flash before doing a request, resulted in an unexpected Exception:
TypeError in ‘SessionController logging in with correct credentials should redirect to the url_after_login url if given’
can’t convert String into Integer
…/vendor/plugins/rspec_on_rails/lib/spec/rails/dsl/behaviour/functional.rb:80:in `flash’
./spec/controllers/session_controller_spec.rb:52:
./script/spec:4:
Read more »
Posted by Andreas on Monday, September 17, 2007 at 06:09 (CEST)
Did you ever want to create multiple models in one action? E.g. if your user model has many email addresses, create a user record and his first email address when he signs up… Nick of Pivotal Blabs wrote a nice article about it – but it almost drove me to despair this morning when I was trying to break it down to my user and email addresses model.
Read more »
Posted by Andreas on Friday, September 07, 2007 at 12:09 (CEST)
Update: I put together the code below and created a rails plugin from it. It’s called
activerecord_symbolize on Github.
ActiveRecord does not natively suppport column types of ENUM or SET. If you want an attribute to act like an ENUM, you’ll most probably use a string and restrict it to certain values using validates_inclusion_of. However, once you’ve got used to Ruby, you’d probably prefer to have symbols as values for these attributes. Here’s a small and easy-to-use snippet that can be used to symbolize values of any ActiveRecord attribute.
Read more »
Posted by Andreas on Monday, August 27, 2007 at 07:08 (CEST)
A few weeks ago, I bought a Cherry eVolution Marlin wireless desktop (it’s a wireless, notebook-like keyboard and a laser-mouse, which are both connected to the PC using one little USB-receiver). Last weekend, when I turned on the PC, I realized that keyboard and mouse didn’t work anymore. Neither keypresses nor mouse movements got to the PC anymore. Looking at the USB-receiver, I saw the red LED flashing rapidly.
It just looks like keyboard and receiver got out of sync and have to be re-connected or re-synced, e.g. like pressing connect buttons. However, neither the receiver nor mouse or keyboard have any hidden buttons or switches to initiate a re-connect and synchronize them.
I couldn’t find any information on the net relating this issue, so I called the support hotline and found out that a rapidly flashing red LED at the receiver does indeed indicate that devices are out-of-sync. A friendly support assistant told me a way how the devices can be connected again.
Read more »
Posted by Andreas on Monday, August 20, 2007 at 13:08 (CEST)
Capistrano 2.0 does not write a revisions.log anymore when deploying your application. However, I liked the revisions.log, because it gives you a clear history of which revision you deployed to your servers at which time. Therefore I wrote a little Capistrano recipe which updates an old-style revisions.log whenever you deploy or rollback your appliaction.
Read more »
Posted by Andreas on Monday, August 13, 2007 at 07:08 (CEST)
I suppose, you know the great scope_out plugin for rails. If not, go check it out, since it’s really great to define DRY scoped associations. Basically, you can use scope_out to define the scoped associations within the model the scope is applied to. However, what I’m missing is a feature like a default scope that can be applied to the model and scopes the default finder.
Read more »
Posted by Andreas on Sunday, July 29, 2007 at 07:07 (CEST)
The more I read about the various new features coming up in Rails 2.0, the more I couldn’t resist… and finally, a few days ago, I switched over to Edge Rails with my current project.
Switching to Edge Rails was easily done, however after fireing up /script/server and doing the first request, I was presented with a 500 Internal Server Error.
DISPATCHER FAILSAFE RESPONSE (has cgi) Sun Jul 29 12:43:56 +0200 2007
Status: 500 Internal Server Error
You have a nil object when you didn’t expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]
/usr/lib64/ruby/1.8/cgi.rb:1165:in `[]’
/usr/lib64/ruby/gems/1.8/gems/gettext-1.10.0/lib/gettext/locale_cgi.rb:26:in `system’
/usr/lib64/ruby/gems/1.8/gems/gettext-1.10.0/lib/gettext/locale.rb:88:in `system’
/usr/lib64/ruby/gems/1.8/gems/gettext-1.10.0/lib/gettext/locale.rb:96:in `default’
After some time of debugging, it turned out to be ruby-gettext, which I use for I18N, that causes a NoMethodError on each request during init-gettext.
Read more »
Posted by Andreas on Saturday, July 21, 2007 at 10:07 (CEST)
The will_paginate plugin for Rails is one of the best pagination plugins I know of and I like the ease of using it. Instead of using Model.find, simply use Model.paginate(:page => x) and you’ll get back a collection that behaves like an array and can be displayed in views like any other collection. Additionally the returned collection has some new methods (like page_count and total_pages), which are useful to display pagination links. Installing the will_paginate plugin adds the pagination class method to all models; it even works with automatic finders: e.g. you can use Model.paginate_by_attr, which will paginate queries for Model.find_by_attr.
However, pagination can become tricky if you want to paginate collections returned by special queries…
Read more »
Posted by Andreas on Wednesday, July 18, 2007 at 11:07 (CEST)
Welcome to my first post in my new Blog.
Finally I managed to get my new Blog online. I’m looking forward to do some posts here from time to time, covering problems or ideas I encounter during my projects (which most probably will be related to Ruby on Rails development or general Linux administration stuff).
As a Ruby on Rails developer myself, I’d rather prefer to run a Blog software using Ruby on Rails – but however, I wasn’t satisfied with the available Blogs based on RoR. Typo is told to be problematic and many people seem to switch to Mephisto. Mephisto however has currently no support for trackbacks and pingbacks. So for now I’ll have to stick to WordPress (which actually forced me to install PHP to my server again, eek).