View that Calculates using Previous Date

I have a table of data that tracks the mileage of my beloved 2005 Toyota Prius.

+----+------------+---------+---------+-------+ 
| id | date       | mileage | gallons | cost  | 
+----+------------+---------+---------+-------+ 
|  1 | 2005-02-14 |     280 |   8.615 | 16.03 | 
|  2 | 2005-02-27 |     480 |   4.775 |  8.59 | 
|  3 | 2005-03-19 |     713 |   7.213 | 14.27 | 
|  4 | 2005-04-09 |     999 |    7.86 | 16.81 | 
|  5 | 2005-04-11 |    1172 |   3.174 |  6.63 | 
|  6 | 2005-05-05 |    1560 |   8.889 | 18.66 | 
|  7 | 2005-06-07 |    1985 |   9.815 | 20.01 | 
|  8 | 2005-07-03 |    2444 |   9.868 |  21.7 | 
|  9 | 2005-08-13 |    2762 |   7.728 |    20 | 
| 10 | 2005-09-11 |    3271 |  10.072 | 30.11 | 
| 11 | 2005-10-24 |    3646 |   8.953 | 22.19 | 
| 12 | 2005-10-30 |    3959 |   6.583 | 17.11 | 
| 13 | 2005-11-04 |    4184 |   4.538 | 11.25 | 
| 14 | 2005-11-21 |    4631 |   8.742 |    18 | 
| 15 | 2005-12-31 |    4897 |   8.511 | 18.64 | 
| 16 | 2006-02-02 |    5609 |    7.83 |    18 | 
| 17 | 2006-03-01 |    6064 |    3.38 |  7.16 | 
| 18 | 2006-03-13 |    6400 |   7.171 |  16.2 | 
| 19 | 2006-03-22 |    6605 |   5.399 | 13.17 | 
| 22 | 2006-02-16 |    5944 |   7.957 |  17.5 | 
| 20 | 2006-04-06 |    6974 |   7.774 | 19.43 | 
| 21 | 2006-04-23 |    7316 |   7.102 | 20.45 | 
+----+------------+---------+---------+-------+

I wanted a way to calculate how many miles were driven for each trip, i.e. between consecutive dates, and what was the miles per gallon (MPG) for that trip.  To do this I modified what I learned in this post, unique ID field, getting next and previous existing ID from table to create a view that does all these calculations.  The advantage of this view is that it does not matter what order I enter the data, it will always calculate correctly.

CREATE VIEW v_prius_mpg AS 
  SELECT id, date, mileage, gallons, cost, mileage as trip_mileage,
         mileage / gallons AS mpg, cost / gallons AS price_per_gallon 
    FROM prius_mpg 
    WHERE id = 1 
  UNION 
  SELECT New.id AS id, New.date, New.mileage, New.gallons, New.cost,
        (New.mileage - Old.mileage) AS trip_mileage,
        (New.mileage - Old.mileage)/ New.gallons AS mpg,
        (New.cost / New.gallons) AS price_per_gallon 
    FROM prius_mpg New, prius_mpg Old 
    WHERE New.id > 1 
      AND Old.id = 
        (SELECT id 
           FROM prius_mpg 
           WHERE date < New.date 
           ORDER BY date DESC 
           LIMIT 1);

And here are the results of the view.  Note how row 20 is out of order but it still calculates correctly the trip mileage and MPG.

+----+------------+---------+---------+--------------+------------------+ 
| id | date       | mileage | gallons | trip_mileage | mpg              | 
+----+------------+---------+---------+--------------+------------------+ 
|  1 | 2005-02-14 |     280 |   8.615 |          280 |  32.501450957632 | 
|  2 | 2005-02-27 |     480 |   4.775 |          200 | 41.8848167539267 | 
|  3 | 2005-03-19 |     713 |   7.213 |          233 | 32.3027866352419 | 
|  4 | 2005-04-09 |     999 |    7.86 |          286 | 36.3867684478371 | 
|  5 | 2005-04-11 |    1172 |   3.174 |          173 | 54.5053560176434 | 
|  6 | 2005-05-05 |    1560 |   8.889 |          388 | 43.6494543818202 | 
|  7 | 2005-06-07 |    1985 |   9.815 |          425 |  43.301069791136 | 
|  8 | 2005-07-03 |    2444 |   9.868 |          459 | 46.5139845966761 | 
|  9 | 2005-08-13 |    2762 |   7.728 |          318 | 41.1490683229814 | 
| 10 | 2005-09-11 |    3271 |  10.072 |          509 | 50.5361397934869 | 
| 11 | 2005-10-24 |    3646 |   8.953 |          375 | 41.8854015413828 | 
| 12 | 2005-10-30 |    3959 |   6.583 |          313 | 47.5467112258849 | 
| 13 | 2005-11-04 |    4184 |   4.538 |          225 | 49.5813133539004 | 
| 14 | 2005-11-21 |    4631 |   8.742 |          447 | 51.1324639670556 | 
| 15 | 2005-12-31 |    4897 |   8.511 |          266 | 31.2536717189519 | 
| 16 | 2006-02-02 |    5609 |    7.83 |          712 | 90.9323116219668 | 
| 17 | 2006-03-01 |    6064 |    3.38 |          120 | 35.5029585798817 | 
| 18 | 2006-03-13 |    6400 |   7.171 |          336 | 46.8553897643285 | 
| 19 | 2006-03-22 |    6605 |   5.399 |          205 | 37.9699944434154 | 
| 20 | 2006-02-16 |    5944 |   7.957 |          335 | 42.1012944577102 | 
| 21 | 2006-04-06 |    6974 |   7.774 |          369 |  47.465912014407 | 
| 22 | 2006-04-23 |    7316 |   7.102 |          342 | 48.1554491692481 | 
+----+------------+---------+---------+--------------+------------------+

I always wanted to appear on every row how many gallons of gas had been consumed up to that day and how much had spent on gas up to date but I could not figure out how.  The only thing I could do was determine the sums up until now.

mysql> select sum(gallons) as gallons_to_date,
       sum(cost) as cost_to_date from prius_mpg where date <= current_date;
+-----------------+--------------+
| gallons_to_date | cost_to_date |
+-----------------+--------------+
|         161.949 |       371.91 |
+-----------------+--------------+
1 row in set (0.01 sec)

Slot not global warnings in ATG logs

In the ATG logs you may see warnings about a slot being session scoped not global.

[STDOUT] Invalid attempt to resolve component /atg/registry/Slots/ActivationFlowSlot in scope global. It is defined in scope session 

Most of the time you can safely ignore these warnings because these warnings are most likely coming from the ACC.

When browsing slots using the ACC the ACC tries to look up the slot as a global component and complains it’s a session scoped component.  In other words it’s an ACC bug that will probably never be fixed since ATG is moving away from the ACC.

Upgrading MeetingKoreans.com to Rails 1.2.6

Today I updated MeetingKoreans.com to Rails 1.2.6 using these instructions and it was quite a frustrating experience. There were three problems that took up most of the time.

  1. No route found to match “/photo/photo/nnn.png”

After upgrading to Rails 1.2.6 I immediately noticed that I was having problems accessing user photos from the database. I am embarrassed to say that this has been a problem on production since February 23, 2007 but until today I had not been able to reproduce this. Obviously the issue was that on production my web host had upgraded Rails but in my development environment I had not.

Previously I was creating the image tag for user photos like this.

  <%= image_tag url_for(:controller => 'photo', :action => 'photo',                        :id => user.primary_photo.id) %>

This would result in HTML like this.

  <img src="/photo/photo/1.png">

This was not a problem before but after the Rails upgrade I would see this error.

  [ERROR] application#index (ActionController::RoutingError)  "no route found to match "/photo/photo/1.png" with {:method=>:get}"

Therefore I changed the RHTML code like this.

  <img src="<%= url_for(:controller => 'photo', :action => 'photo',                        :id => user.primary_photo.id) %>"

This would result in HTML like this.

  <img src="/photo/photo/1">

Fortunately this solved the problem.

  1. undefined method ‘model’ for ApplicationController:Class

When I first did the upgrade I did not do the final step.

  rake rails:update:configs

This did not seem to be a problem at first when I ran it into the development mode. But when I ran it in production I saw errors like this, apparently because it was trying to run the application as a Rails 2.0 application.

  Status: 500 Internal Server Error

  undefined method 'model' for ApplicationController:Class

   /meetingkoreans.com/app/controllers/application.rb:13

Fortunately after running the rake update these errors no longer appeared.

  1. undefined method ‘model’ for ApplicationController:Class

After solving the above problem I immediately started seeing these errors.

  Status: 500 Internal Server Error

  Expected /meetingkoreans.com/app/controllers/mk_controller.rb to define MkController

    /usr/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/

    dependencies.rb:249:in `load_missing_constant'

This one really puzzled me and as I searched around I learned that apparently many others were puzzled too. In this bug report, Expected x.rb to define X error, I found within the comments a work around from saizai that worked for me.

I suspect this setting in /config/environments/production.rb: config.action_controller.consider_all_requests_local = false

I changed this to ‘true’, restarted the server, and it worked. I then set it BACK to ‘false’, restarted… and it still worked.

So, I don’t really know for sure WHY it started working again or how this might be causing it, but hopefully it helps.

I have no idea why that worked but I am glad it did.

  1. Conclusion

I have been a web application programmer for over ten years and though I am still excited about Ruby on Rails I must admit this experience has lowered my opinion of it.  I have never seen upgrades to the platform cause applications to break so catastrophically like this and I have orchestrated migrations of major web applications before.  I shudder to think what the upgrade to Rails 2.0 will be like.

ruby: no such file to load — ubygems (LoadError)

Today when I started my Ruby on Rails application I received this error.

> ruby script/server
ruby: no such file to load -- ubygems (LoadError)

After Googling for this error message I realized somehow my Ruby gems installation was corrupted. I had been planning on upgrading my Ruby from 1.85 to 1.86-26 so now seemed as a good time as ever. 🙂

Fortunately after installing the latest version of Ruby and updating to Ruby on Rails 1.2.6 everything seemed fine.

Shop.com Product Display Integration

Last year for Casual Male I did the Shop.com Product Display Integration (PDI) which allows Casual Male to sell its products on Shop.com.

The integration involved three steps.

  1. Access all the products organized by category using ATG’s GSA Repository.
  2. Export the products into an XML file according to the PDI DTD using the XStream library.
  3. FTP upload to Shop.com’s servers using Apache Commons Net library.

Upgrading to Rails 1.2.6

Today I upgraded to Rails 1.2.6 as I prepare to move towards Rails 2.0. Notes on this release can be found here.

Here are the steps I took.

  1. Upgrade Ruby Gems to the latest version (in this case 1.0.1).
    gem update --system
  2. Upgrade Ruby on Rails to the latest version (in this case 2.0.2).
    gem install rails
    To specify upgrading to Ruby on Rails 1.2.6 do this.
    gem install rails -v 1.2.6
    (What I probably should have done was a gem update because now I have installed 1.2.6 and 1.2.3.)
  3. Update configuration.
    set RAILS_GEM_VERSION to ‘1.2.6’ in config/environment.rb
    rake rails:update:configs