Array of nil results returned by Thinking Sphinx 2
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 1
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 1
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
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 10
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 3
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
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
Mongrel Mailing List Search
Rails Day 2006
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
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
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!
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
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

