MarkUs Blog

MarkUs Developers Blog About Their Project

Archive for the ‘OLM on Rails’ Category

Migrate Your Tests

without comments

Rails has a nice way of maintaining versions of your database schema through ActiveRecord:Migration.  This lets you modify your existing schema without the hassle of manually copying the same schema to all the different deployed instances that you have, and does this automatically for you.

When I usually change the schema, I usually drop all the tables and recreate them using:

rake db:migrate VERSION=0
rake db:migrate

and then repopulate my development DB environment using a script I use.  However it turns out that this doesn’t migrate your test environment automatically with it. I tried creating a simple test of making sure that my ActiveRecord validation works.  However, I ended up getting this error:

test_numericality_group_limit(AssignmentTest):
NoMethodError: undefined method `group_limit=' for #<Assignment:0xb7242018>
/var/lib/gems/1.8/gems/activerecord-2.1.0/lib/active_record/attribute_methods.rb:251:in `method_missing'
test/unit/assignment_test.rb:11:in `test_numericality_group_limit'
/var/lib/gems/1.8/gems/activesupport-2.1.0/lib/active_support/testing/setup_and_teardown.rb:33:in `__send__'
/var/lib/gems/1.8/gems/activesupport-2.1.0/lib/active_support/testing/setup_and_teardown.rb:33:in `run'

After hours of finding the bug, I found out that you have to migrate your test DB environment as well, by executing the following:

rake db:schema:dump
rake db:test:prepare

This will copy the schema that you have right now in your development environment, and copy it to your test environment.

Written by Geofrey

August 6th, 2008 at 10:49 am

Posted in Uncategorized

Phase 1: Complete

without comments

This week, I’ve finally managed to wrap up the student management module in rails. I had some leftover work done from implementing the authentication and session timeouts, as well as writing several tests. Since there were several complaints in OLM before from TAs being timed out, we’ve decided to set different timeout lengths for students and faculty, giving longer timeouts for TAs to finish their work (although it wouldn’t hurt to save once in a while too).

Next, I moved on to implementing the page for managing the course classlist. This involved doing some parsing work for the uploaded CSV file. This took me a while since I was under the assumption that the given classlist would be separated by spaces. I’ve started looking for CSV parsers where you can change the default delimiter (you’d think this would exist). Since the search was in vain, I just implemented my own parser only to find out that there was no way to read the uploaded file line-by-line unless I write it first to a tempfile. By this time, Karen already sent me a sample classlist only to find out that it was indeed a normal CSV file (whew!). So I just used the rails CSV parser and everything works as planned.

The problem I have now is how to test uploading the file automatically. Fortunately, rails has something called a fixture_file_upload where you can just specify a sample upload file and call a post method on your action. I haven’t successfully tried to use this though, since I’m still getting some errors when running it. I’m running out of ideas why my tests are failing. I also ran into some problems dealing with error handling when trying to enter invalid inputs on creating and editing students. Turns out that the symbol declared in the form expects a global variable with the same name in order to hook them up together (who knew?). Things that are automatically done like this without telling you is where sometimes rails becomes a pain. Sometimes, declaring an innocent variable might be something that would override the default rails way of doing things. But all that hard work seemed to pay off once you get everything working (except for the upload tests).

Now, I was suppose to get the student file submission done by the end of the week, but I’m sort of glad that I didn’t. My initial design draft focused only on the uploading the file draft without even considering the next step: student groups. As it turns out, I might need to implement them at the same time to have a cleaner design with less hassle. The plan is to get a database schema that I can present to Karen and Jennifer by next week, together with some user stories on how partnerships will be done.

For now, I think I’ll continue reading up on my new Rails book, as well as watch Randy Pausch’s last lecture, which Luke hails as “simply amazing and inspiring”.

Written by Geofrey

August 1st, 2008 at 10:20 pm

Posted in Uncategorized

Pay Attention to Little Things

with one comment

On testing:

If you ever tried running your tests and find yourself getting the error:

your_controller_test.rb:3:in `require': no such file to load -- test_helper (LoadError)
from your_controller_test.rb:3

it’s probably because the generated require call, require test_helper is not being properly recognized.  Instead of the default require 'test_helper', replace it with:

require File.dirname(__FILE__) + '/../test_helper'

If you’re doing functional testing, make sure that the controller being tested is also required (e.g. add the line require 'your_controller' on top).  Be sure that you also have installed the package libtest-unit-ruby (if using Linux), before testing.

Also, thanks to Jeff Balogh, make sure that you append test_ on all your test cases to make sure that they actually run (I know it’s obvious, but it’s nice to be reminded once in a while).

On database constraints:

If you want to enforce foreign key constraints, remember to create the required indexes first.  ActiveRecord Migration supports this using add_index.  This only applies if the column being referred to is not a primary key, if it is then there’s no need to create the index. Once this is set, you can now enforce foreign keys using ActiveRecord Migrations by implementing this.

On implementing sessions:

By default, the rails session handler stores session information on a file in the server.  This can cause scalability issues in the long run if file space runs out or when using multiple servers.  Thus, it’s recommended to use :active_record_store instead. To do this make sure that you do the following:

  • execute rake db:sessions:create to add a create table migration for sessions
  • execute rake db:migrate to apply the new schema
  • change session store to active session store by adding this line to config/environment.rb:

config.action_controller.session_store = :active_record_store

  • Make sure that you comment out or add this line to your ApplicationController:

protect_from_forgery :secret => <32_hex_digit_secret_key>

Here are also some helpful links regarding sessions and implementing it:

Written by Geofrey

July 29th, 2008 at 4:01 pm

Posted in Uncategorized

You know you're a Java Programmer…

without comments

…pretending to be a RoR programmer, if:

  • You keep typing “nill” (null) instead of nil
  • You think private and protected keywords apply only to the method, thus spending the whole afternoon scratching your head and asking why your action is not being found.
  • You keep using extends on modules and don’t know that there’s an include as well (and don’t know the difference between them).
  • You feel uneasy comparing Strings with == and uses eql? instead (even if they’re the same)
  • You think ruby symbols is just Matz’s way of messing with your mind (like Matrix did)
  • You think Rails is WebObjects on steriods…and acid at the same time 🙂

Written by Geofrey

July 25th, 2008 at 9:05 pm

Posted in Uncategorized

Getting Started with Ruby on Rails

with 2 comments

Update: March 13, 2009

I’ve tried installing rails again from scratch, from a newly installed Ubuntu 8.10 and I got a “no ruby-full package” error when I tried to execute the install command sudo apt-get install ruby-full rubygems build-essential

If this happens, you can enable external package repositories by going to System -> Administration -> Software Sources.  Then you can go to Third-Party Software tab and select both sources (‘http://archive.canonical.com/ubuntu intrepid’ as of Ubuntu 8.10) and do a sudo apt-get update after on the command line. Once update is done, installing ruby-full package should now work.

———-

Here, I outline the steps I took to get started with Ruby on Rails development on Ubuntu 8.04

1.  Install ruby

To install ruby (1.8.6) and rubygems, simply type:

sudo apt-get install ruby-full rubygems build-essential

2.  Update gem

When installing rails, make sure that you let gems manage it rather than apt-get, otherwise, “gem and apt-get will start fighting like Spock and Captain Kirk in Amok Time.”  Thus, we need to make sure rubygems is updated using the command sudo gem update (for gems 0.9.4)

Now, you might or might not encounter this but when I did this on Ubuntu 8.04, I got the following error:

Updating RubyGems...
Bulk updating Gem source index for: http://gems.rubyforge.org
Attempting remote update of rubygems-update
ERROR:  While executing gem ... (Gem::GemNotFoundException)
Could not find rubygems-update (> 0) in any repository

Asking the all-knowing Google lead me to this fix.  You apparently need to remove the local source_cache file found in the GEM PATH.  You can get this path by running the command gem env, giving you this result:

$ gem env
RubyGems Environment:
- VERSION: 0.9.4 (0.9.4)
- INSTALLATION DIRECTORY: /var/lib/gems/1.8
- GEM PATH:
- /var/lib/gems/1.8
- REMOTE SOURCES:
- http://gems.rubyforge.org

Once you get your GEM PATH (in my case /var/lib/gems/1.8), then you need to remove the local cache by typing:

sudo rm -rf /var/lib/gems/1.8/source_cache

and continue to update gem.

3. Install rails

Now that you have gem updated, you can then confidently install rails and its dependencies by typing:

sudo gem install rails --include-dependencies

Make sure that you also add the gem bin path to your path. If you’re using bash, then add the following line to your .bashrc, replacing <GEM_PATH> with your gem path found by typing gem env (don’t forget to restart terminal):

export PATH=$PATH:<GEM_PATH>/bin

Verify installation by typing, rails -v and get the following (or similar) response:
Rails 2.1.0

4. Install a Database

Since I already have a MySQL server, I wanted to work with it instead of the default SQLite database for Rails.  This means installing the MySQL driver for rails and typing the command:

sudo apt-get install libmysql-ruby mysql-server

You can leave out the mysql-server if you already have the MySQL server installed.

5. Install a Ruby on Rails server

Reading some of the posts, I found that there are two most popular servers to use for RoR apps: Mongrel and Webrick.  Webrick I believe comes with the rails installation, but there’s huge evidence that Mongrel might be better than Webrick.  Using the steps found here, I installed Mongrel on Ubuntu by typing:

sudo gem install mongrel --include-dependencies

You might be given options to select versions.  In my case, I just selected the latest version of mongrel (1.1.5) and fastthread (1.0.1) for ruby.

6. Create sample application

To make sure everything works, we can create a sample rails application.  Start by typing rails cookbook -d mysql if you’re using MySQL, otherwise leave the -d flag out to use SQLite instead.  The database configuration assumes that you have a MySQL user called root, with the password disabled.  If this is not the case, then you might need to edit the config file cookbook/config/database.yml and make appropriate changes under development. To make sure the database also works, we need to create a sample application that uses our database. To do this, just type in console under the cookbook directory:

ruby script/generate scaffold recipe title:string description:text category:string

mysql -u root --password=<your_password> -e "CREATE DATABASE cookbook_development"

rake db:migrate

Once that’s done, just execute mongrel_rails start under the cookbook directory, fire up your browser to http://localhost:3000/recipes (note that it’s plural) and you should see your cookbook web application staring back at you.

Note: If you get a CGI::Session::CookieStore::TamperedWithCookie error when hitting the recipes page, just hit browser refresh and that should be fixed (not sure why it does that sometimes).

7. Find a good Ruby on Rails IDE (no, this is not optional)

Now the hard part of all this is getting settled into a familiar development environment.  There are some good discussions out there on what people use as RoR IDEs.  Since I don’t have a week to throw away learning emacs, I’ve decided to start with something familiar, Eclipse.  The only decent Eclipse-based IDE out there is RadRails, but installing the standalone verion, I found that it was too alpha for me at this stage (crashes frequently, unresponsive, and can’t find my rails installation).  So I’m settling right now with the Rails plug-in for NetBeans and so far it’s been good for me, except for the occasional GUI lag that I’m getting. I’ve also heard good things about jEdit, but without the debugger support, it’s not technically an IDE for me.

Written by Geofrey

July 17th, 2008 at 4:32 pm

Posted in Uncategorized

Goodbye WebObjects, Hello Rails

without comments

It’s officially over. With five weeks left, I’m done doing WebObjects development for Web-CAT, and switching to a new project.  The powers-that-be have decided to focus on OLM instead and supplementing it with features that it lacks.  For now, this would involve taking over student submissions and group associations that would work in parallel with OLM.  It’s pretty rare to start a project from scratch, so I really want to tread carefully with design and testing on the forefront, especially that this is my first time using RoR as well.

Developing this on RoR would serve two purposes.  Since I only have five weeks left, we need some rapid prototyping environment to put this all together and have something to show at the end of summer to see if our understanding of submission and group workflow does indeed work.  The second purpose is to quickly assess if RoR is indeed a suitable platform for us given the requirement of plugability for the features that we want.  If all of this works by the end of summer, we could then perhaps start porting OLM bit by bit over to the rails application.

It’s a bittersweet goodbye, but we learn what we can from our mistakes and hope to do better the second time around.

Written by Geofrey

July 16th, 2008 at 5:19 pm

Posted in Uncategorized