Archive for November, 2009

Generate Rails Migrations from your PostgreSQL or MySQL database

Thursday, November 26th, 2009

1) Create a new empty Rails project called schemer

2) In your config/database.yml file, point at the database you wish to dump to a migrations file

3) Run the command ‘rake db:schema:dump’. This should create a db/schema.rb file. Amazingly this effectively is your migrations file!

4) To tidy up create a file called file db/migrate/20091125205635_create_initial_schema.rb

5) Then copy the create_table statements from the schema.rb file into the new file 20091125205635_create_initial_schema.rb. Here’s a template

class CreateInitialSchema < ActiveRecord::Migration

  def self.up
    # Put all create_table statements from schema.rb file here
    # Note: You don't need the 'ActiveRecord::Schema.define(:version' line or it's enclosing end statement
    # ...
    # ...
  end

  def self.down
    # Don't really need this
  end

end

6) Once you’ve all this done you can just run ‘rake db:migrate’ and you should have a new sqlite db up and running under db/development.sqlite3

Thanks to Justin Ball on this Nobody Listens Anyway blog at Dump an Existing Database Schema Into a Ruby On Rails Migration Ready Format for the basis of this tip. Sometimes somebody does…

Eight Useful Git Tips

Sunday, November 22nd, 2009

1.) You can check your config using git config -l

This tells you useful stuff like what remotes you’re bound to, etc.

2.) You can blow away files listed as ‘untracked’ (by the git status command) using git clean (be careful!).

If you want any of those files to hang around (but not appear as ‘untracked’) then add them to your  .gitignore file.

3.) You can re-enter your last commit using git commit --amend. What’s nifty is that you can change files (including file addition and deletion) as well as the commit message. Just do what you gotta do before running the amend command and enter a replacement commit message.

4.) You can undo your last commit completely using git reset HEAD^

*** This does not change the working tree ***

Alternatively, you can use the syntax git HEAD~2 instead of HEAD^^

5.) Refer to the last revision by HEAD, the second last as HEAD^ and the third last as HEAD^^

You can keep adding carrets forever! Sounding like Bugs Bunny there.

6.) You can see the contents of a file from a revision using

git show rev:path/to/file

7.) Ok, this tip is a bit of a longer one. Use git merge some_other_branch to merge another branch into the one your working on. (Note that git pull does a merge automatically once it’s completely – if you don’t want this to happen use git fetch). There are a few possible outcomes:

Fast-forward merge : If changes were made on only one of the branches since the last merge, then the merge should happen without any need for you to act.

Three-way merge: If changes were made on both branches, then git decides how to merge them using an algorithm. If any changes conflict with each other, then git will report them and let you resolve them. Once you’ve fixed any conflicts then you can git commit.

If there were no conflicts, git automatically does a commit with a stock commit message for the log. If you don’t like the idea of this then use git merge --no-commit some_other_branch. You need to manually do the commit afterwards. (This is more like how other distributed VCS’s such as Mercurial. This was the tip that inspired this post!).

8.) Finally, one tip that I found useful is that you don’t want to git pull into your repository when you have uncommitted changes. Especially if you treasure your sanity. Instead git stash is your friend. Here’s how
* Do a git stash to move your uncommitted changes to a magical place in git temporarily
* Then do the git pull
* And finally do a git stash apply. This will now move your ’stashed’ uncommitted changes back (from the magical temporary place in git) into your working directory and take into account what what was changed by the pull. Neat!

Tips 1-7 taken from:  Git – SVN Crash Course. Tip 8 taken from painful experience :-)

If you want a good detailed yet friendly reference on a particular command search the Git User Manual on kernel.org.

Getting Contact Form Emailing working with CForms in Wordpress

Friday, November 20th, 2009

“Oh thou horrid little hard-coded piece of Javascript.” That’s our take on the jumping through hoops required to get this little plugin emailing this week. Here’s why…

On uploading the wordpress site from a development machine (which had the cforms plugin installed locally) to a server online, we could not get a contact form created with CForms to send out an email. Instead, it would say “One moment please” but alas that moment would never end. We tried TLS settings and permission changes but nothing worked. Fortunately there was a solution which we came across at the plugin’s somewhat squashed forum.

When we had developed the site locally, the plugin had hardcoded a variable ’sajax_uri’ into the /wp-content/plugins/cforms/js/cforms.js. This is what was causing the pesky problemo and a quick edit of that file to point the variable at a correct URL for our server fixed the issue.

Hmm… I’m sure there’s a good reason is not a variable in a config somewhere. There’s a few hours of my evening I’ll never get back…

Installing Ruby On Rails on Windows

Thursday, November 19th, 2009

There were a couple of great outcomes from the first Free Ruby Lesson we ran in the Havana cafe on Dublin’s Georges St last Monday. The first was getting everyone hacking with the Rails stack and some practical examples in double quick time. The second was managing to get RoR installed on Windows Vista as the lesson rumbled on in the background. Here’s how Rails conquered Vista.

We kicked off by following the instructions on the rails wiki. It’s worth paying careful attention to anywhere it says to set path variables. If you find that you are getting an error that says that you version of rubygems is not recent enough (and you can’t get rubygems to update itself) then you can install an older version of Rails, for example 2.3.2, using the following from the command line

gem install rails -v 2.3.2

(Note: If you have already installed a different version of rails you can uninstall it first by using ‘gem uninstall rails’).

The big problem however was getting sqlite3 working. We did install the SQLite Command Line Tool and the SQLite DLL as the Rails Wiki instructions said to – but to no avail. We kept getting a popup error saying that the sqlite3 dll was not found and to please consider reinstalling. Fortunately, we gambled on one of the answers on StackOverflow. Basically the solution was from the command line

gem uninstall sqlite3-ruby
gem install sqlite3-ruby --source http://gems.rubyinstaller.org

And with that we were away! The final thing was to get a nice friendly IDE installed so we plumbed for Netbeans on the Netbeans Downloads page. Look out for the special Ruby version of Netbeans which is one of the links around the middle of the downloads page.

Anyway, that’s all for this week. Happy Ruby hacking!

Offline Documentation for Rails (and other Ruby gems)

Thursday, November 12th, 2009

I used to pretty much always install gems with the –no-ri –no-rdoc options to speed up installation. Recently however, I’ve found myself needing to get access to documentation whilst on the move. So just in case you don’t know how that works, here’s the deal.

* Install your gems as per normal (ie. don’t use –no-ri or –no-rdoc)
* Run the command ‘gem server’ from the command line
* Browse to http://localhost:8808
* And voila! You should have all the docs you need available by clicking on the rdoc link for any given gem
* But if you really want to get fancy check out the searchable Rails documentation at http://railsapi.com – there’s an online version as well as the downloadable (the links are at the top of the page and can easily be mistaken for an advert!)