Array of nil results returned by Thinking Sphinx 5

Posted by Ben Reubenstein Thu, 31 Dec 2009 16:02:00 GMT

Sphinx combined with Thinking Sphinx is a powerful full text search solution for Rails. After using it on several projects, when it started returning an array of nil results, I was perplexed to say the least. After adjusting the model I found the issue to occur when I set an alternate primary key in the model using set_primary_key. When that is set, Sphinx works returning the proper IDs, the SQL executes properly, but the search method returns [nil, nil,....].

According to the Sphinx documentation:

ALL DOCUMENT IDS MUST BE UNIQUE UNSIGNED NON-ZERO INTEGER NUMBERS (32-BIT OR 64-BIT, DEPENDING ON BUILD TIME SETTINGS).

To fix, set_sphinx_primary_key must also be set. In this particular case the table still had the standard id. Do the following in the model:

  set_primary_key :other_primary_key
  set_sphinx_primary_key :id

$PATH when Using Passenger (mod_rails) aka BJ does work with Rails 4

Posted by Ben Reubenstein Wed, 18 Feb 2009 00:25:00 GMT

Phusion Passenger has become my default Rails setup lately. Today I had issues when using BJ on a production box, and it came down to two issues. The first was Bj not working quite right with Rails Time. The gist of that fix is to change every reference of Time.now to Time.now.utc. The next however was tougher to track down. I was getting error messages in my email:

no bj found in ["RAILS_ROOT/script", "/sbin", "/usr/sbin", "/bin", "/usr/bin"]

I jumped into the console, and ENV["PATH"] reported the correct paths, including /usr/local/bin. It turns out that passenger inherits the $PATH of apache, so I manually set the path in environment.rb and my problem was solved.

ENV['PATH'] = "#{ENV['PATH']}:/usr/local/bin"

RHEL5 Getting The Rails Console Working 2

Posted by Ben Reubenstein Tue, 10 Feb 2009 16:42:00 GMT

If you compile Ruby from scratch you may get this error when starting a script/console session:

`require': no such file to load -- readline (LoadError)

To solve this you need to install the readline ruby extension. Here I assume you are using the 1.8.7 Ruby.

sudo yum install ncurses-devel readline-devel
cd ~
mkdir src
cd src
wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p72.tar.gz
tar zxvf ruby-1.8.7-p72.tar.gz
cd ruby-1.8.7-p72.tar.gz/ext/readline
ruby extconf.rb
make
sudo make install

iNetwork Test Launches

Posted by Ben Reubenstein Tue, 10 Jun 2008 01:47:00 GMT

After almost a year of collecting data and half a million results recorded, Xcellent Creations, Inc. has released the next version of the iNetwork Test web application. The iNetwork Test web application for iPhone has been moved to http://www.inetworktest.com/iphone_content. The main iNetworkTest.com is now a portal to Mobile Network Testing Results.

User's can now create an account which allows them to save all of their results and create a personal results map. This build out will also support new versions for the Android platform and the iPhone.

attachment_fu Now With Local File Fu 15

Posted by Ben Reubenstein Fri, 04 Jan 2008 17:50:00 GMT

In the beginning there was file_column. It was an excellent plugin for handling file uploads and image processing with the added bonus of being able to simply pass a file to it and have it work without a file upload via a form. One thing that file_column didn't do was fill in your db with file attribute goodness that could be used to create logic around a particular file. attachment_fu handled this along with the ability to use multiple image processors. For detailed info on attachment_fu, check out Mike Clark's article.

In order to add some local_file_fu to attachment_fu so you can pass a local file directly to it, you have to take your local file and turn it into a temporary file that you can pass to attachment_fu's uploaded_data method. I altered the solution outlined here for my solution.

1. Create a class in your models directory in a file called local_file.rb.

require 'tempfile'
class LocalFile
 # The filename, *not* including the path, of the "uploaded" file
 attr_reader :original_filename
 # The content type of the "uploaded" file
 attr_reader :content_type

 def initialize(path)
  raise "#{path} file does not exist" unless File.exist?(path)
  content_type ||= @@image_mime_types[File.extname(path)]
  raise "Unrecognized MIME type for #{path}" unless content_type
  @content_type = content_type
  @original_filename = File.basename(path)
  @tempfile = Tempfile.new(@original_filename)
  FileUtils.copy_file(path, @tempfile.path)
 end

 def path #:nodoc:
  @tempfile.path
 end
 alias local_path path

 def method_missing(method_name, *args, &block) #:nodoc:
  @tempfile.send(method_name, *args, &block)
 end
end


2. In order for attachment_fu to pass validations, you need to set the mime type of the file. This would usually come from the form when it is uploaded, but since we are using a local file, we'll set our mime types in environment.rb. At the end of the file add the various mime types you will need:

@@image_mime_types ||= { ".gif" => "image/gif", ".ief" => "image/ief", ".jpe" => "image/jpeg", ".jpeg" => "image/jpeg", ".jpg" => "image/jpeg", ".pbm" => "image/x-portable-bitmap", ".pgm" => "image/x-portable-graymap", ".png" => "image/png", ".pnm" => "image/x-portable-anymap", ".ppm" => "image/x-portable-pixmap", ".ras" => "image/cmu-raster", ".rgb" => "image/x-rgb", ".tif" => "image/tiff", ".tiff" => "image/tiff", ".xbm" => "image/x-xbitmap", ".xpm" => "image/x-xpixmap", ".xwd" => "image/x-xwindowdump" }.freeze

3. Now in your code that creates the model that has_attachments you can simply do the following:

model = Model.new()
model.uploaded_data = LocalFile.new(FULL_PATH_TO_FILE)
model.save


As always, comment on anything you have issues with or suggestions.

Debug Output For Migrations in Ruby on Rails 4

Posted by Ben Reubenstein Tue, 31 Jul 2007 13:04:00 GMT

Today I was doing some massive migrations that rocked the very foundations of an app I am working on. Problem was that during the migration I was moving objects around the database and validations were failing for my saves. To get some debugging going, I started out trying to write to the logger, with some good ol' logger.debug, but that does not work within a migration. To output information in a migration, use puts. So for example to see all the errors that prevented object g from being saved:
class MyMigration < ActiveRecord::Migration
  def self.up
    g = G.new(:name => "name" )
    if g.save
      #More object manipulation here
    else
      g.errors.each do |x|
        puts x
      end
    end
  end

  def self.down
    #Down Method Code Here
  end
end

Google Co-op Custom Search Engines

Posted by Ben Reubenstein Thu, 07 Jun 2007 11:33:00 GMT

When Google announced the new Google Co-op, which includes custom search engines, I did not immediately see the value. I wish I understood this services huge value sooner! I was trying to configure a reliable Apache with Mongrel Clustering setup, but was having a very tough time finding information. The Mongrel-users mailing list is hosted using Mailman, which does not have an obvious search function. When querying Google I was getting those results mixed in with the rest of the web. This is a perfect example of how a custom search engine can narrow your scope and get you the right answer fast. I setup a simple custom engine that just searched http://rubyforge.org/pipermail/mongrel-users/. I used the same query I had been using within Google, but this time found my answer almost immediately.

I have started another Ruby on Rails dedicated search engine that I hope to refine as I find more sources of information. This one searches the major Rails/Ruby mailing lists, the wiki, and the main site. I have left the search engine open, so if you have a site to contribute, feel free!

Ruby On Rails Custom Search
Google Custom Search


Mongrel Mailing List Search
Google Custom Search


Rails Day 2006

Posted by Ben Reubenstein Sat, 17 Jun 2006 01:15:06 GMT

So I did end up turning in an app for the Rails Day Contest! I thought with moving there was no way, but I spent about 3 hours and created a very simple app I call, My Name Is URL... You can watch the competition real time here and see my progress here. Good Night, and Good Luck everyone who is participating in Rails Day!

Typo Upgrade 3

Posted by Ben Reubenstein Thu, 04 May 2006 21:39:00 GMT

I just updated benr75 to run off of the Typo trunk. The move went relatively smoothly, but with a few minor issues. The first issue was solved pretty quickly as DeLynn Berry upgraded recently too, and posted this fix for many older templates that were not using the right method to render the sidebar.

My next issue I must say got my heart racing for a minute. The database changed a great deal since the last release, and the migration for some reason did not set the type or name fields for my pages. After some quick SQL, I changed the NULL type fields to Page in the Contents table, and fixed the NULL name fields back to their original name.

As a bonus the commenting is now working properly!

Rails Quick Reference

Posted by Ben Reubenstein Thu, 04 May 2006 16:47:58 GMT

Jens-Christian just posted an excellent PDF he titles, Ruby on Rails Short Reference on his InVisible Blog. It contains a good chunk of information on many aspects of Ruby on Rails including rake, generators, models, controllers, and views.

Party like it's 1.1!

Posted by Ben Reubenstein Tue, 28 Mar 2006 09:24:00 GMT

Ruby on Rails 1.1 was released today. If you already have Rails installed, simply run:


gem install rails --include-dependencies
rake rails:update

It is also recommended that ruby is upgraded to 1.8.4. On my windows box this caused all kinds of issues with how tab characters were interpreted. I posted this tip to the Rails mailing list:

Hello All ~

As suggested I upgraded Ruby to 1.8.4 along with Rails. My Linux boxes have been running 1.8.4 and haven't complained about my source. On the Windows version however, if you start to see errors like:

Invalid char `\002' in expression

parse error, unexpected tIDENTIFIER, expecting kEND

Go through your code and replace your tabs with spaces. I use EditPlus and you can configure the tabs to use spaces. This solved my problem.

~ Ben

Also, if you're running Lighttpd, be sure to upgrade.

Userstamp Plugin Available

Posted by Ben Reubenstein Fri, 20 Jan 2006 16:33:00 GMT

DeLynn has finnally released his Userstamp Plugin for Rails. In a nutshell if you place fields in your database named updatedby and createdby it will fill the values in for you when the particular model is saved with the current user. Obviously there are some more technical things to do to get it to work, but I tested it in my app and it works as described. Instructions and source can be found here:

http://www.delynnberry.com/pages/userstamp