First Steps

The initial spec. for this project is to write a Rails application that stores the office lottery pool picks in a simple database, allowing for multiple users (a future capability), and allows the user to enter additional picks.  The user will then be able to enter the current draw and the results will be displayed.  Not the most complex application in the world, but covering some basic functionality.

First off, I created a MySQL database, and added a table for my picks.  This required me to edit the database.yml config file to change it from sqlite3 to mysql, which is how the rails command left it.  I subsequently found out that there’s a command line switch that allows you to generate a config with mysql, but…   I used a tinyint for one of the boolean columns.  Now some of you maybe thinking, why didn’t I use the generator for all of this?  The answer is I didn’t know about that stuff. I generated the application with the rails command.  I generated a controller, and a model.  I popped a simple action in the controller to test everything, being careful to match the case-sensitive model name (I hate case-sensitive languages!):

 def list_picks
   @list=Megapick.find_by_sql("select * from mega;")
 end

and created a view with <%= debug(@list) %> What could be simpler? It all failed, so I tried rake db: migrate, and restarted the server.  This now worked, so I started messing around with the query, perhaps I should use find(:all)?  So I tried, that didn’t work, which was worrying.  Dug around a bit and found something that said I should add a reference to the database table in the model set_table_name "mega".  That fixed it right up, but I was obviously doing something in the wrong way.

I poked around some more and found out about “scaffold”.  Scaffold was the method for building the framework connecting the models to the database.  You used to use it inline, but the recommended way was to use the generator.  I scrubbed the existing model and table ran the scaffold generator with a new model.  That failed.  I had used a model name that was plural.  That’s a big no no.  Fixed that, remembering to check the routes.rb file, and lo!  I had a simple Model, Controller, View.  All model searches were working, I could display the search results in the view.  I could control how they were formatted.  I had joined up all the dots.

Now I realize that I had gone about all of this in the wrong way, but there something about having a “hello world” application that covers some important aspects of web application programming.  I started planning in my head, the way I was actually going to structure this application.  Little did I realize that my “fat application” developer background was going to be thrown a loop.  Coming up next: developing in a stateless environment.

Leave a Reply

Your email address will not be published. Required fields are marked *