1. Skip to navigation
  2. Skip to content

The ELC Community Blog

A knowledge exchange on Ruby on Rails and Agile Development

February 24, 2007

by stevend

Installing RMagick properly in OSX

I've seen the craziest problems with people installing the RMagick gem before. In this article, I will show you the procedure I use to fix any and all rmagick problems. First, I start by cleaning out any of the following packages:

   1  sudo port uninstall imagemagick
   2  sudo port uninstall graphicsmagick
   3  sudo port uninstall ghostscript
   4  sudo port uninstall freetype
   5  sudo gem uninstall rmagick

Then, I reinstall them:

   1  sudo port install freetype
   2  sudo port install ghostscript
   3  sudo port install imagemagick
   4  sudo port install graphicsmagick
   5  sudo gem install rmagick

This software cocktail has solved the following problems:

  • RMagick gem won't compile native extensions
  • RMagick can't find fonts - This is a really common problem, and we have seen a case where this approach will not solve this problem.
  • RMagick renders text incorrectly and/or unreadable in the validates_captcha plugin

ImageScience is an alternative to RMagick which requires fewer native libraries, but not zero unfortunately. It may or may not be easier for you to use, depending on what you need RMagick for (it won't do text rendering, for example).

February 24, 2007

by stevend

RubyGems 0.91 and the "refresh" error

All of us over at ELCTech recently upgraded to RubyGems 0.91, only to find the following error during "gem" operations:

   1  $ sudo gem install acts_as_ferret
   2  ERROR:  While executing gem ... (NoMethodError)
   3      undefined method `refresh' for #<Hash:0x127c854>

Apparently the format of the gem cache files have changed, the new version cannot read some older versions of the cache. To fix this problem, delete your source_cache files in the following locations:

   1  sudo rm /usr/local/lib/ruby/gems/1.8/source_cache
   2  rm ~/.gem/source_cache

Looks like someone forgot to think about us old folks with ancient versions of RubyGems. In the future, I'll plan on running "gem update --system" more often!

February 20, 2007

by Ryan Garver

ActiveRecord attribute update semantics

ActiveRecord (AR) provides a number of great features. One feature that is often taken for granted is the mapping of table columns (attributes) to class signals. We use this feature of AR without thinking about it.

  
   1  article = Article.find(1)
   2  article.attributes[:title] #=> "A Blog Posting"
   3  article.title #=> "A Blog Posting"

We also can take this several steps further and modify how AR responds to requests for the value of a attribute. Jamis Buck had a recent blog post outlining an interesting way to set up default values for an attribute than can depend on another attribute. The difficulty in AR comes when you need to modify what happens when attributes are being updated. A great example of this is if you want an action to always happen when (say) you assign an

   1  Author
to
   1  Article.author
. AR has several different ways of getting and setting attributes out of the database. We'll focus on the setting since that is often the confusing part of AR hacking. You should pay attention to the underlying implementation mentioned below; it's easy to accidentally create infinite recursion.

   1  article.attributes[:attr_name]=value
Every AR object has an
   1  attributes
Hash that contains the
   1  name=>value
pairs that correspond to the columns and their values for the object. This Hash is bi-directional and therefore can be modified. Once the
   1  attributes
Hash has been updated you can save the AR object and those updates will persist. Much of the alternative methods of updating AR objects are built on this base. The
   1  attr_name
can be either a string or a symbol.
   1  article.attr_name=value
This assigns
   1  attr_name
with
   1  value
within the in-memory AR object. To commit the change you must call
   1  save
on the AR object. This ends up calling
   1  []=
under the hood. When overwriting how the attributes are updated in a AR object you'll want to override this method and do the final assignment using
   1  []=
.
   1  article.update_attribute(:attr_name, value)
This assigns the
   1  value
to
   1  attr_name
and saves the value to the database all in one go. Behind the scene this calls the
   1  article.attr_name=
to do the assignment which means that you can override how this assignment takes place. This method of assignment also skips validation on the attribute.
   1  article.update_attributes(:attr_name1 => value1, :attr_name2 => value2, ...)
This method is the same as calling
   1  []=
for each
   1  name=>value
pair and then saving.

A quick note. When overloading the assignment of association do not try to do the overloading on (for example) the

   1  author=
method, but rather the
   1  author_id=
method. Otherwise you confuse AR and it doesn't call your methods.


home | services | Ruby on Rails Development | code | blog | company