RSS icon Home icon
  • Rails has_many Time.now gotcha

    Posted on October 7th, 2009 dryan No comments

    It’s one of those face palm moments for me, but don’t use has_many in your rails models if it’s time dependent data. In the production environment the Time.now will be the time the environment loads the class, which is only once per server start. Keep in mind your code will work in development and test mode. So basically the solution is to move those has_many statements into a function.

    has_many :recent_stuff, :conditions => ["starts_at =?", Time.now]

    becomes…

    def recent_stuff
    timenow = Time.now
    self.find(:all, :conditions => ["starts_at = ?", timenow])
    end

  • authlogic facebook-connect and rspec

    Posted on September 22nd, 2009 dryan No comments

    The past week I’ve had a chance to add facebook-connect to a rails site we’ve been working on by using the fantastic Authlogic and the Authlogic Facebook Connect add on.  However I found that all my existing controller tests blew up after adding the facebook-connect add on and after a little digging realized I needed to stub a method on the  rspec TestRequest method.

    The TestCase rdoc explains how to setup authlogic within your rspec controller tests, so I won’t get into that here.  Since I already had everything setup with a login method in my spec_helper.rb file, I just had to add a single line to that method.

    ~/spec/spec_helper.rb

    def login(user)

    request.stub!(:set_facebook_session)

    UserSession.create(users(user))

    end

    Eventually I’ll have to write some tests for any features that use the facebook api, but for now stubbing that method does the trick.

  • Mac OS X Snow Leopard + Rails, MySQL, and Sphinx

    Posted on August 26th, 2009 schmeeve 13 comments

    Ah, Snow Leopard. While you’re admiring the photoshop job Apple did to the box art, you may be eager to install the new Leopard, but hang on if you’re using Rails.

    If you’re getting the following types of error with the mysql driver, you may need to reinstall.

    The error:

    !!! The bundled mysql.rb driver has been removed from Rails 2.2. Please install the mysql gem and try again: gem install mysql.
    rake aborted!
    dlopen(/Library/Ruby/Gems/1.8/gems/mysql-2.7/lib/mysql.bundle, 9): no suitable image found. Did find:
    /Library/Ruby/Gems/1.8/gems/mysql-2.7/lib/mysql.bundle: mach-o, but wrong architecture - /Library/Ruby/Gems/1.8/gems/mysql-2.7/lib/mysql.bundle

    The fix:

    sudo env ARCHFLAGS="-arch i386" gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config

    (h/t here)

    Sphinx/Ultrasphinx:

    If sphinx is already installed (and searchd starts up without issue), you may be set. However, if you’re installing it new, you’ll want to compile it in 32-bit mode:

    Download the latest tarball of Sphinx here.

    ./configure CFLAGS="-O -arch i386" CXXFLAGS="-O -arch i386" LDFLAGS="-arch i386" --disable-dependency-tracking
    make
    make install

    RMagick:

    NOTE: Despite trying this, I still am having issues with RMagick. If anyone’s got tips…

    UPDATE: As per Dimitri’s (big hat tip!) post below, I *finally* got everything going using MacPorts, which I hadn’t realized was updated for Snow Leopard yet.

    Didn’t go so smoothly, but finally succeeded. Just threw in the towel and got my old MacPorts install out of the way and started over.

    To uninstall MacPorts completely (big caveat: this will uninstall everything previously installed with MacPorts):

    sudo port -f uninstall installed
    
    sudo rm -rf /opt/local \
    /Applications/MacPorts \
    /Applications/DarwinPorts \
    /Library/Tcl/macports1.0 \
    /Library/Tcl/darwinports1.0 \
    /Library/LaunchDaemons/org.macports.* \
    /Library/StartupItems/DarwinPortsStartup \
    /Library/Receipts/MacPorts*.pkg \
    /Library/Receipts/DarwinPorts*.pkg \
    ~/.macports

    Then installed libxml2 and ImageMagick as Dimitri recommends, followed by:

    sudo port install rb-rubygems
    sudo gem install rmagick