Archive for April 2011

Workaround for corruption in saving bytea data thru rails

I’m not sure where the bug is, but when saving some binary data that was generated in a Rails3 before_create callback, it kept getting truncated in the actual INSERT INTO statement (though it appeared fine even in after_save callback, it was truncated in the database). Using PostgreSQL 8.4.7 with pg (0.9.0) and activerecord 3.0.5

Seemed like it could be related to this fixed bug, but my problem is on the save itself:
https://rails.lighthouseapp.com/projects/8994/tickets/611-cannot-write-certain-binary-data-to-postgresql-bytea-columns-in-2-1-0

In any case, found a simple workaround: uuencode the data first, and uudecode on loading.

   before_create do
      ... stuff that builds my_hash ...
      self.my_hash = Base64.encode64(Marshal.dump(my_hash))
   end

Then later, to reconstitute the hash,

   loaded_hash = Marshal.load(Base64.decode64(@record.my_hash))

Perl equiv to PHP’s md5(uniqid(rand(), true)

Trying to shoehorn some data from the back into the wp_connections database, I needed a perl equiv to the md5(uniqid(rand(), true) function. From some general suggestions at http://www.experts-exchange.com/Programming/Languages/Scripting/Perl/Q_26773756.html came up with the following snippet:

use Digest::MD5 qw (md5_hex);
use Data::Uniqid qw (suniqid uniqid luniqid );

sub gen_token {
# approx equiv to md5(uniqid(rand(), true));
return md5_hex(uniqid);
}

Will also need to use Scott Hurring’s http://hurring.com/scott/code/perl/serialize/ to get it fully into the needed format…

activescaffold column overwrite – ugly

Perhaps this is somehow due to my config…but if it happens to me, it could happen to you:

I have a model, say table_one with a belongs_to: table_two. It happens that table_one and table_two each have a column named ‘datafile’. The two columns are intended to refer to entirely separate datafiles, and thru lack of imagination I named both the same. Now when I edit a row from table_one, it shows the associated row from table_two, and uses an identical name for the input fields for both ‘datafile’’s! record[datafile]

The result is that the datafile for table_one is wiped out on edit, as it is replaced by the blank datafile input for the associated record for table_two. Ouch!

Workaround of changing the name of the column in table_two fixes the problem.

Using activescaffold 3.0.5 for Rails 3.0

best practice for getting things – a hell of a lot of things – done

Seems to me the key is creating a language that lets you efficiently express what you need to. Well-designed dsl’s (domain specific languages) have a lot of power. But these guys are the ones that apparently know:

http://www.readwriteweb.com/hack/2011/01/secrets-of-backtypes-data-engineers.php

Rails docs!

Found a real effort to doc the rails framework! Not all filled in, but a huge improvement over the tutorial-as-doc approach:

http://apidock.com/rails

ie link_to

Thanks APIDock!

How to override ‘Create New’ for activescaffold associations

In the helper file, ie tablenames_helper.rb, override the render_action_link method

 def render_action_link(link, url_options, record = nil, html_options = {})
    if link.parameters[:association] == :my_association
       # special handling here
    else
       super  # in case you have other associations for which you do want the default behavior
    end
 end

This confounded me for a while as it was easy to create a helper to override the link to associated records, but if the records did not yet exist the ‘Create New’ appeared hardcoded. After inspection of ./bundler/gems/active_scaffold-45451d963672/lib/active_scaffold/helpers/list_column_helpers.rb and ./bundler/gems/active_scaffold-45451d963672/lib/active_scaffold/helpers/view_helpers.rb the above override became apparent. Is there an easier way to determine this sort of thing?

The problem with important authors…

…is that they are too important to take time for the grunge work that makes a technical book useful.

Agile Web Development with Rails is essentially a book-length tutorial, and imho not worth either the money or time. The entire book is narrative. It admits as much in the introduction: “This book isn’t meant to be a reference manual for Rails…reference manuals are not the way most people learn.” Maybe so, but they are extremely helpful productivity tools when trying to do actual work! (The narrative itself is so patronizing and self-aggrandizing its painful to plow thru, though probably does have some value if you can stomach it.) An auto-generated code API is no replacement for a human edited reference manual with context. It seems Rails requires you to ‘read the code not the docs’ in order to do anything nonstandard, which works, but…not exactly productivity enhancing as advertised.

Browsing a good reference lets you see immediately the structure of a framework.

PHP.net is beautiful and so efficient, I’ve never bought a PHP book either – but for the opposite reason I’d advise not buying this one.

case of the missing layout

After a bit of refactoring of one of my controllers in a Rails3 app, the layout mysteriously disappeared, with

Rendered vendor/plugins/active_scaffold/frontends/default/views/list.html.erb (188.5ms)

replacing

Rendered vendor/plugins/active_scaffold/frontends/default/views/list.html.erb within layouts/application (192.1ms)

in the Rails debug output. Turned out to be a simple error – I added a routine to initialize some instance variables and forgot the ’super’ at the end to call the parent class initializer:

def initialize
   ... my instance vars ...
   super
end