Migrations With Referential Integrity

Posted by Max Dunn Tue, 15 May 2007 18:12:24 GMT | no comments

Migrations don’t come with referential integrity directives, best as I can tell. There are some plugins but it appeared to me (ok, after only a brief review) that they were more focused on adding referential integrity for rails object hierarchies (:belongs_to etc.), so that seemed both overkill and insufficient for what I needed to do.

But it turns out that an “execute” statement in the migration lets you issue sql directives directly.

create_table "dvectors", :id => false, :force => true do |t|
  t.column "length",      :integer, :default => -1
  t.column "fvector",     :binary
  t.column "document_id", :integer, :default => -1
  t.column "total_size",  :integer, :default => 0
end
add_index "dvectors", ["document_id"], :name => "document_id" 
execute "alter table dvectors add constraint `dvectors_fk` " +
        "foreign key (`document_id`) references `documents` (`id`) on delete cascade"

(Submitted by Wido Menhardt)

Posted in

Typo Google Sitemap fix

Posted by Max Dunn Tue, 16 Jan 2007 17:12:48 GMT | 3 comments

I guess Google used to be more lenient in the format of their Sitemaps. However, when I installed Typo 4.0.3 and submitted a sitemap, Google didn’t like the format.

The format of the Google Sitemap is straightforward:

 <?xml version="1.0" encoding="UTF-8"?>
  <urlset xmlns="http://www.google.com/schemas/sitemap/0.84">
   <url>
    <loc>http://www.example.com/</loc>
    <lastmod>2005-01-01</lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.8</priority>
   </url>  
  </urlset> 

However, Typo 4.0.3 and Typo edge (as of 15-Jan-2007) both used “item” instead of “url” and “link” instead of “loc”.

Luckily the fix is easy. Edit all 4 _googlesitemap_item_*.rxml files in /app/views/xml and change “xm.item” to “xm.url” and “xm.link” to “xm.loc”. For instance, _googlesitemap_item_article.rxml will look like:

xm.url do
  xm.loc post_link(item)
  xm.lastmod item.updated_at.xmlschema
  xm.priority 0.8
end

Now make similar changes to the other 3 files. When done, go into Typo Admin, Settings, Cache and select “Rebuild cached HTML”. Now you can submit a sitemap like http://blog.maxdunn.com/sitemap.xml to Google Sitemaps and it will work!

Posted in

Differences between RewriteRule in .htaccess and httpd.conf

Posted by Max Dunn Fri, 12 Jan 2007 23:08:34 GMT | 8 comments

This is probably obvious to people who know Apache well, but I just spent a half day struggling with the differences between RewriteRule in .htaccess and httpd.conf. I was assuming they would behave the same way, but there are slight differences:

Differences

httpd.conf

  • The request URI will have a leading slash
  • The leading slash needs to remain to find cached files
  • When using -f or !-f, it needs to have the document root on front

.htaccess

  • The request URI won’t have a leading slash
  • Can’t have a leading slash to find cached files
  • When using -f or !-f, don’t use the document root

Examples

Here is an example that looks in the public/cache directory for cached files:

httpd.conf

RewriteRule ^/$ /cache/index.html [QSA]
RewriteRule ^/([^.]+)$ /cache/$1.html [QSA]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

.htaccess

RewriteRule ^$ cache/index.html [QSA]
RewriteRule ^([^.]+)$ cache/$1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

Rails

Just to complete this example, add this line to a before_filter method in application.rb to have Rails cache files to this directory:

ApplicationController::page_cache_directory = "#{RAILS_ROOT}/public/cache"

Posted in ,

Installing MySQL Ruby Gem on Mac OS X 10.4

Posted by Max Dunn Wed, 13 Dec 2006 07:14:38 GMT | 12 comments

There are some problems installing the Ruby Gem MySQL on the Mac 10.4. When you do a:

sudo gem install mysql

you will likely get an error like:

*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

To fix this problem, run this:

sudo gem install mysql -- --with-mysql-dir=/usr/local/mysql

(Solution From How to Install on OSX)

Next, you will probably see this error:

mysql.c: In function 'Init_mysql':
mysql.c:2015: error: 'ulong' undeclared (first use in this function)
...

To fix this, change to /usr/local/lib/ruby/gems/1.8/gems/mysql-2.7 and edit “mysql.c” to add this to the top:

#define ulong unsigned long

(Solution from: I.NFECTIO.US)

Now recompile with:

sudo make
sudo make install
sudo make clean

That should do it!

Posted in ,

"svn: Malformed network data" error in Eclipse

Posted by Max Dunn Thu, 07 Dec 2006 18:37:18 GMT | 12 comments

Several weeks ago, I changed from a Windows laptop to a MacBook. After I got Eclipse setup and went to check-in some code to RubyForge, I received an error:

svn: Commit failed (details follow):
svn: Malformed network data

At first I figured that this was just a problem with the RadRails Subclipse module on my Mac, but then I tried it on my old Windows computer that used to work fine, and got the same error!

After googling around, it seems that this is a known issue when SVN server is updated to version 1.4, but older clients are used. The clients were fixed in October, but there hasn’t been a new official Subclipse build so the automatic Eclipse update doesn’t help. I spent a day trying to figure out how to compile a new Eclipse version, but no luck.

However, there are two workarounds. First, the svn command line works fine.

The second workaround relies on the fact that the changed files are actually committed, but that the error just prevents the local svn repository from recognizing that. So after the error, doing an “update” will merge the changes and everything will be good.

If you have added or deleted files, however, the update trick won’t work. In this case, it is necessary to delete the entire directory that contains the added or deleted files. Be sure to do this outside of Eclipse with Explorer or Finder! Then update the files and it will bring everything down fine.

Versions

Program Version
RadRails v.0.7.1
Subclipse 1.0.3
svn command line 1.3.1

Fix

Mark Phippard added a comment explaining the problem. Here is a summary:

The issue is JavaSVN. It had a problem talking to Subversion 1.4.x server. RadRails can be used with Eclipse 3.2 and then you can install Subclipse 1.1.9 which includes SVN 1.4 clients.

So I installed Eclipse 3.2 and RadRails on top. However, when I installed Subclipse according to the instructions:

  1. Eclipse: Help => Software Updates => Find and Install
  2. Search for new features to install
  3. New Remote Site
  4. URL => http://subclipse.tigris.org/update_1.0.x

it gave me version Subclipse 1.0.3. The trick to get Subclipse 1.1.9 was to use the update site:

  • URL => http://subclipse.tigris.org/update_1.2.x

November 2008 – New Fix

Well the “malformed data” problem just cropped up again! But the fix is the same as above – just update Subclipse to 1.4.6 using:

  • URL => http://subclipse.tigris.org/update_1.4.x

Posted in ,

Ruby on Rails Camp Was A Success

Posted by Max Dunn Mon, 20 Nov 2006 20:15:56 GMT | 1 comment

Wow, the Ruby on Rails Camp really went well!

Beforehand, our biggest fear with this unconference format was that only a few people would want to lead a session. So our hearts stopped momentarily when when we called for sessions and only 6 people initially came down. But after a few minutes, a bunch of other people started coming down and we quickly filled up the schedule with 28 sessions.

Besides the great sessions, the speed demo was very popular.

People were also very impressed with the beautiful IBM facility and terrific food and drinks they provided, as well as the free coffee barista and sushi dinner made possible by our other sponsors. Thanks again to IBM for hosting the event and providing all the food and refreshments, and our other sponsors for making this possible!

We had a lot of great feedback about the camp. Greg Tomei said “Wow – my expectations were exceeded by a huge measure!” and Dav Yaginuma said “The very first session on memcached was perhaps the most perfect conference session I’d ever attended.”

But the real thanks should go to all you attendees – it was your efforts that really made this unconference a success!

Posted in

Ruby on Rails Camp

Posted by Max Dunn Tue, 10 Oct 2006 15:54:47 GMT | no comments

It’s here! Ever since Wido and I went to the MashupCamp in July, we wanted to do a similar unconference for Ruby on Rails (RoR). So we have been looking around for locations and figuring out dates, and now have everything finalized. The Ruby on Rails Camp is going to be November 9th at IBM Almaden in San Jose, California.

Is Ruby on Rails Ready for Business?

The focus will be on whether RoR is ready for real-world, real-business applications. Will it scale? Will I be able to hire developers? Will it persist? Will there be support? Will it be compatible with my other applications? Will there be enough third-party components? Will I be able to get funding? These are all issues we hope to talk about, as well as other general Ruby and Rails topics.

Unconference

We are calling it a “camp” rather than a “conference” because it will be held in the Unconference style. So instead of having preset speakers and sessions, the first thing that will happen is that we will ask the participants to decide what sessions they would like to give themselves.

There will also be a SpeedDemo sessions so that participants can show off their own RoR applications – whether they are commercially polished or just something hacked together for fun. This will be a great way to see what you can do with RoR!

Fee

We debated on whether to charge a fee and decided on a small fee of $25 to reduce the number of no-shows. We also are planning to have Kaliya Hamlin facilitate the camp, and the fee will help make that possible.

Signups

So if you are developing a Ruby on Rails business application – or would like to – or are just interested in learning more about Ruby on Rails, you should definitely come to the Ruby on Rails Camp!

Posted in

Apache Redirects to the Rescue

Posted by Max Dunn Sat, 07 Oct 2006 19:36:12 GMT | no comments

There are a couple of technical problems with my web site. The first is that to get into this blog, you need to add the trailing slash at the end of “typo” like this:

http://www.maxdunn.com/typo/

Otherwise, the wiki gives a page not found error for the “typo” page. Of course, when most people type in the URL manually, they won’t add the trailing slash and then get confused when it doesn’t pull up my blog.

I have tried various things in the Rails routing.rb file to correct for this, but none worked. However, a simple change to .htaccess did the trick:

RedirectMatch permanent ^/typo$ http://www.maxdunn.com/typo/

Now another problem I have is that my ISP Site5 maps maxdunn.com to www.maxdunn.com and there is no way to turn this off. This causes several problems:

  • When logged into www.maxdunn.com, you won’t be logged in if you access it as maxdunn.com.
  • If someone first accesses the site using maxdunn.com, then the page cache will create all links as maxdunn.com and serve these up, even if the page is later accessed as www.maxdunn.com.
  • If the site is accessed with both maxdunn.com and www.maxdunn.com, then Google and other search engines will think these are two sites and neither will be ranked as highly.

To fix this problem, there is another simple .htaccess addition that will redirect all maxdunn.com requests to www.maxdunn.com:

RewriteCond %{HTTP_HOST} ^maxdunn.com$ [NC]
RewriteRule ^(.*)$ http://www.maxdunn.com/$1 [L,R=301]

Posted in ,

Ruby on Rails Advanced Page Caching

Posted by Max Dunn Sat, 16 Sep 2006 20:02:37 GMT | 4 comments

Ruby on Rails (RoR or Rails) has some cool caching mechanisms already built in. You can cache parts of pages (fragment caching) or whole pages (action caching) while still going into your Rails code to check for permissions or do other things. However, for the highest performance, you can use page caching which serves the web page without even starting Rails, making it incredibly fast. For instance, on my slow computer, doing action caching was 2 times faster than normal, but doing page caching was 30 times faster! (See Ruby on Rails Caching Benchmarks).

However, the problem I ran into was that when you are logged into my wiki as an editor, you see an “Edit” link on each section of the page, and if you are logged in as an admin, you see an “Admin” choice on the menu. So while the Rails action caching would work fine for this, I couldn’t use the Rails out-of-the-box page caching since if I was logged in as an Editor and browsed through the pages, the next person that looked at the pages would still see all the “Edit” links, even if they were not logged in!

After some thought, I came up with two solutions for solving this problem. The first was based on rewriting the URL depending on what role the user is logged in as. I then came up with a better method that relies on setting a cookie with the current role and then using Javascript to modify the page appropriately. Here are the details on both methods.

Read more...

Posted in

Ruby on Rails Better Wiki URLs

Posted by Max Dunn Sat, 16 Sep 2006 18:36:04 GMT | no comments

Normally links in a wiki would appear like this:

  • http://www.maxdunn.com/show/Ruby+on+Rails

However, I wanted the URLs in my wiki to look more like web pages:

  • http://www.maxdunn.com/Ruby+on+Rails

It is simple to do this and here are the details:

Read more...

Posted in

Older posts: 1 2