17 août 2009

RubyZip and MS Office .docx

To create a .docx Word document, you need to zip up a number of files. If you use the rubyzip gem (as seems natural), you'd get a nasty warning:

"Microsoft Office cannot open this file because the .zip archive file
is an unsupported version."


Instead, use the command-line zip command and it works like a charm. See another post describing a similar problem.

attr_accessor and timestamps

I had an attribute set with attr_accessor. Then in a before_update, I would set an actual DB field on certain conditions like so:


def before_update
if ....
self.my_db_field = self.my_attribute_from_attr_accessor
end


The problem I encountered is that, if the attribute in attr_accessor is the only one that is modified, the updated_at timestamp is not changed...

If you look at the lifecycle of an ActiveRecord update, I think the timestamp update occurs some time BEFORE the before_update...

Too bad! A simple workaround, though, is to put the code above in the validation, as this definitely gets executed before the timestamp update.

30 juillet 2009

schema_search_path in Postgres adapter

I've found that problem when using the Postgres adapter (and Rails 2.1.1) with multiple schemas (because I am not using the default Postgres user to connect to the database).

The error you get is:

PGError: ERROR: relation "schema_migrations" already exists


The root cause is that the Postgres adapter returns no table names, because the schemas it looks into are like this: 'schema1', ' public' (the extra space before public is the problem)

I've found one post that accurately describes the problem

The easy fix is to add a schema_search_path definition in config/database.yml like so:

schema_search_path: public

19 juillet 2009

IE6 in quirks mode

Is Internet Explorer running in quirks mode or standards mode?

It turns out there is a very simple way to tell. Just add in your address bar:

javascript:alert(document.compatMode)

If the browser responds with: "CSS1Compat, it is in standards mode. If it responds with "BackCompat", it runs in quirks mode. See this link for more details.

In the latter case, you probably want to change your DOCTYPE, for instance to:

http://www.w3.org/TR/html4/loose.dtd">

See this link for a useful list of DOCTYPEs

26 juin 2009

ArgumentError after upgrade to Rails 2.3.2

If you get error:

ArgumentError: wrong number of arguments (3 for 0)

in your tests after upgrading to Rails 2.3.2, maybe you have a method called find_layout that determines the layout to use?

If so, change the name to something else (I named mine my_layout) and magic, it works.

It must be a name conflict somewhere in the new Rails...

Rails assets and Apache SSL

In a current project, I use calls to assets.mycoolsite.com to improve load times. However, we only have one server with one IP address, so ultimately all calls point to the same IP address.

This becomes important when you start using SSL. Unfortunately, Apache cannot recognize 2 different NameVirtualHost at the same IP address in SSL mode... Bummer!

Rails to the rescue!

  1. Install Asset hosting with minimum SSL from his majesty DHH himself:
    script/plugin install git://github.com/dhh/asset-hosting-with-minimum-ssl.git

    (Warning though: it requires a version of Rails > 2.2, so you have to be ready to upgrade: see README for more details)
  2. In production.rb:
    config.action_controller.asset_host = AssetHostingWithMinimumSsl.new(
    "http://assets%d.example.com", # will serve non-SSL assetts on http://assets[1-4].example.com
    "https://assets1.example.com"  # will serve SSL assets on https://assets1.example.com
    )