Category Archives: Rails

Ruby on Rails

MissingAttributeError from within after_initialize

I found this interesting. If you use an after_initialize hook in an ActiveRecord model for some reason and you hit upon one of the attribute accessor methods, the following exception may reveal itself.

ActiveRecord::MissingAttributeError: missing attribute:

Specifically, I came across it when referencing the has_many side of an association from within a belongs_to model. For [...]

Rails date validation freak on blank

When I have a date that can be nil, I usually end up needing the following code, which I’ve monkey patched into ActiveRecord::Base:

private
 
def string_to_date(value)
value = case value
when blank? then nil
when Date then value
else
begin
Date.parse(value) rescue nil
rescue
nil
end
end
end

Afterward, I simply use the awesome validates_timeliness plugin with :allow_nil => true.

Sensible remoteSort for Ext JS

If you’re managing complex model relationships like I am on the server side, you might find you need to sort on multiple columns to maintain correct ordering or perhaps because Ruby on Rails uses SELECT DISTINCT ON internally which exhibits unpredictable results when combined with PostgreSQL and LIMIT.
Presently, Ext JS will simply send the name [...]

Rails, json_serializer, associations, and you

While the JSON support in Rails has improved, at least as of 2.1.0 the serializer doesn’t take into account model overrides of #to_json. Fortunately, a patch has been introduced, although it has been staled out.
Rails 2.3.3 and possibly earlier versions of Rails 2.3 no longer have this issue.
In the meantime, I have been defining [...]

Amazon’s Call Me Support Option

I haven’t ever had to call Amazon before, but apparently at some point they implemented a callback feature as the preferred method of contacting support by phone. It’s provided by a service called eStara. It seemed like an interesting idea, at first. The touted benefits, that the support representative always knows who [...]

Ext JS Factory pattern for HttpProxy

An immediately application of the Factory pattern involves Ext.data.HttpProxy. Since initial configuration is all that is likely necessary, not extension, a Factory is perfect. For example, Ruby on Rails needs the Accept header to be set to ‘application/javascript’ for its magic to work when rendering a reply.

Ext.ns(’liaison.util’);
 
liaison.util.HttpProxyWithOptions = function(config) {
return new Ext.data.HttpProxy(Ext.apply({
headers:{’Accept’:'application/javascript’}
}, config));
};
 
var [...]

Rails 2.1 JSON and mootools 1.2 Request.JSON

The short answer is no. Fortunately, the lengthy answer involves a compatibility library for mootools and a few additional options to legacy Json.Remote from mootools 1.11.
First, you’ll need to obtain the mootools 1.2 compatibility library, which includes an implementation of Json.Request that behaves in a manner conducive for sending JSON to an application server.
Next, [...]

Rails STI resouce routes routing

After hours of Googling and an ill timed browser reload while typing this the first time, I finally happened upon RoR ticket 10454 which details the problem I am having with inferred routes and STI models. I modified it to supposedly support any level of model nesting. Seems to work now. I [...]

has_many :through and with_scope protected

If you were following the excellent has_many :through blog, you may find you can’t use with_scope in the methods defined on your association. Instead, just send :with_scope along.

has_many :active_campaigns, :through => :campaign_distributions do
def

ed2k helper with Ruby

Nothing fancy. It works.

#!/usr/bin/ruby1.8

# ed2k link helper for Opera

host = ‘10.10.1.1′

link = ARGV.first

require ‘net/telnet’
core = Net::Telnet::new(
‘Host’ => host,
‘Port’ => 4000
)

core.cmd(”dllink #{link}”)
core.cmd(’q')
core.close