Tuesday, December 8, 2009

undefined method `reflect_on_all_associations'

Using Rails 2.3.5 , I started recieving this error when running all unit tests on an older application.:


1) Error:
test_truth(CommitmentTest):
NoMethodError: undefined method `reflect_on_all_associations' for Object:Class


1 tests, 0 assertions, 0 failures, 1 errors


The problem was happening when Rails was trying to load fixtures. Removing all the fixures removed the error. After adding back a few at a time, I narrowed the problem down to the Fixtures for an object called Content :

The Content object has a field called type, which is reserved for Single Table Inheritance. renaming that field solved the problem.

The vague undefined method `reflect_on_all_associations' can have a variety of sources, In general, it means there's a problem with a Model object.

See Also:
http://iridescenturchin.wordpress.com/page/2/
http://sourceforge.jp/projects/rubycocoa/lists/archive/devel/2008-January/001293.html

Wednesday, September 16, 2009

undefined method `assert_difference'

I struggled with this error after upgrading an older application to Rails 2.3 . assert_difference has not left, but it took me a while to find out why I got this error. The parent class TestCase now lives in the ActiveSupport module.

Old code:

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

class UserTest < Test::Unit::TestCase

Solution:

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

class UserTest <
ActiveSupport::TestCase

Monday, February 16, 2009

Code Generation using Rails, Rake and ERB

Two great things about Ruby are it's brevity and it's dynamic nature. A great way to introduce Ruby to a project written in another language is some simple code generation.

Say we need some XML files that represent data currently stored in a database. To make it simple, our desired file will use the table name pluralized as the parent element, and contain nested singular elements, one for each record in the database. Within the record elements, each field name is an element, and the value of that field is the text value of that element.

<?xml version="1.0" encoding="UTF-8"?>
<bonds xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="bonds.xsd">
<bond>
<issuer>GENERAL MOTORS CORP</issuer>
<rate>0.072</rate>
<coupon>Fixed</coupon>
<maturity>1/15/2011</maturity>
<type>US Corporate Debentures</type>
<price>19.5</price>
<yield>1.29582</yield>
<callable>true</callable>
<frequency>Semi-Annual</frequency>
<first_payment>7/15/2001</first_payment>
</bond>
<bond>
<issuer>FEDERAL NATL MTG ASSN</issuer>
<rate>0.045</rate>
<coupon>Variable</coupon>
<maturity>2/17/2012</maturity>
<type>US Agency Retail Note</type>
<price></price>
<yield></yield>
<callable>Yes</callable>
<frequency>Semi-Annual</frequency>
<first_payment>2/17/2005</first_payment>
</bond>
</bonds>

Let's generate this file from Rake.

1. Generate a new Rails application by typing rails code_generator (or whatever you wish to call your helper application)
2. Change RAILS_ROOT/config/database.yml to point to your applications DB

development:
adapter: sqlite3
database: db/development.sqlite3
timeout: 5000



3. Create a file named something like generator.rb in RAILS_ROOT/lib/tasks
4. Require ERB at the top of this file.
require 'erb'


5. Add a method to send the contents of a table to an ERB template named table.type.erb . Notice the second to last line of the method below. It's where we pass the binding method to ERB. this will make all our variables and methods available to the ERB template we create later, including the dataset variable and the table variable.


def process_erb(type,table)

# Grab our ERB template
template = ERB.new(IO.readlines("lib/tasks/table.#{type}.erb").to_s

# Use Active Record to get our dataset from the DB
dataset = ActiveRecord::Base.connection.execute("select * from #{table.underscore}")

# Create a new file for the ERB output
file_name = "#{file_name}.#{type}"
f = File.new("#{file_name}",'w')

# Write the ERB output to the new file:
f.puts(template.result(binding))
f.close
end


6 . Add a rake task to call our method to create an XML file.
TABLE = ENV['TABLE']

task :create_xml => [:environment] do
process_erb("xml",TABLE)
end


7. All we need is the ERB template named table.xml.erb We write this taking advantage of the dataset and table variables available to us from the ruby binding.

<<%=table.underscore.pluralize%>>
<% dataset.each{|row| %>
<<%=table.underscore.pluralize%>>
<% row.each{ |k,v| %>
<<%=k%>><%=v%></<%=k%>>
<% } %>
</<%=table%>>
<% } %>
</<%= table.underscore.pluralize %>>
8. Our method expects a table name at the command line , so we'll call it with
rake create_xml TABLE=bond

The result is a bonds.xml file with our desired data!

9. Notice how our template name contains our desired file type, xml, then our method takes this as a parameter. How easy will it be now to create a table.java.erb template, and call it? How about a template called table.php.erb ?

10. How about running this generator from jRuby to access an Oracle database?

development:
adapter: jdbc
driver: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@oracle.apptrain.com:1521:xe
username: test





Friday, February 6, 2009

Informational Websites


An Informational Website is a collection of web pages, images, text and other digital assets that are all hosted on web servers. The pages of a website can be accessed from a URL called the homepage. The URL organizes them into a hierarchy. The hyperlinks between them control how viewers perceive the image and how the traffic flows between the different parts of the site.


Home Page

|

Main Section

^

Sub Sections

^ ^

More Sub Sections

Examples of basic Informational Sites created by AppTrain Software are Allenlock.com and AppTrain.com .



Be seen on the internet!


Informational Websites are an effective and exciting way to promote your business. AppTrain designs all websites so they can easily grow and change along with your company. Contact AppTrain to be seen on the internet.

Wednesday, January 21, 2009

The To-do List

I find myself taking a lot of hand written notes to organize my thoughts. Making a To-do List is a simple solution to staying organized. Writing the list by hand makes it concrete and more real.


Listing Agendas, Assignments, and other needs is a great technique to better organize tasks by staying efficient. Everyday I can overview what I have done and what needs to be done. I never imagine something so simple like making a written list could have such a tremendous impact on my efficiency throughout the day. This posting started as a plan on my To-do list.


To-do List Tips:
· Name the category or topic something clear and short (1 or 2 words)
· Rank or write in the order of importance (color code or numbering)
· List smaller sub-categories (assign pages to that specific task)
· Establish due dates or time frames
· Cross out executed jobs ( Fold pages of previous list & rewrite)



Wednesday, January 7, 2009

Renaissance Programming



Integrating Ruby Rails, PHP, MATLAB and The Google Docs API for KpozSports.


The Renaissance Programmer is productive in multiple programming languages and platforms. Like the Renaissance Architect, He understands what's important when building software; the symmetry and simplicity of the end product.





The initial challenge when first working for KPozSports was that the existing site was written in PHP . I've been working mostly with Ruby and Rails for the past few years. And Jim on our team is a talented PHP developer. So how can Ruby and PHP developers work together on the same site?


1) Have a Clear Division of Responsibilities.

On any project, each developer needs to work toward specific functional goals. The language or tools they use to meet these goals is ancillary. Although KPoz is a small company, we don't mind working with multiple languages to get our jobs done. Rob is an expert with the MATLAB programming language. All of our computationally intensive work is done in MATLAB. The PHP code is primarily there for the Web interface. Jim's been building web sites for years, and it's his tool of choice. Adding some Ruby code to the project didn't bother anyone because they were more interested in the resulting functionality. My primarily responsibility was to gather data from several sources, organize it, and integrate the PHP site with the MATLAB background processes. Ruby, known as The Enterprise Glue, was a natural choice.



2) Group Similar Goals and Tasks together.


Software developers call this cohesion. KPoz gets it's game data from Rake tasks I've written that do screen scrapes from public data sources on the web. When the task came along to screen scrape current point spread estimates for those same games, it was assigned to me. My mind had recently been absorbed in the problem, and I was able to reuse some existing code.



3) Keep Components Independent.

We built a complete internal site in Rails to manage data flow for KPoz. The site runs independent of the External PHP site, and the MATLAB components. Conversely, The PHP and MATLAB components don't require the existence of the other components. They barely know about each other. Software developers call this Low Coupling. This makes it easy to work on and run a component without worrying about breaking the other components.



4) Design Together.

Building a single usable software application requires constant design. Everyone at KPoz participates in the design process. We keep important concepts defined in an online Domain Dictionary that continuously gets revised. This ensures that developers talking about the same concepts are using the same terms. Keeping a harmonious Domain Dictionary will ultimately resonate into the code, and out to the end user.



5) Don't Repeat Yourself!


The full DRY rule, from The Pragmatic Programmer is
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

When data is shared across databases, it needs to flow correctly, from the authoritative representation to the replica. In our system, the authoritative representation of our object model is the Rails database. Updates to that database automatically replicate throughout our system. This unambiguous flow of data keeps things from getting complicated. In addition to keeping our own code DRY, it's important to make sure that the system as a whole is DRY, particularly when integrating multiple sub systems.



6) Constant Communication.


Developers working on disparate platforms from different physical locations need to proactively engage each other. At KPoz we have two weekly calls. At the beginning of the week we thoroughly discuss plans, strategy (or is it tactics?) , tasks, and issues. The end of the week call is a quick stand-up meeting. By having two weekly calls, even if someone is travelling or busy , we all speak at least once a week. We also frequently contact each other informally , for anything from discussing a fascinating programming problem, getting a remedial lesson in Statistics, or celebrating a favorite Football Teams victory.



7) Expand Your Mind.

In Pragmatic Thinking and Learning, Andy Hunt discusses the concept of Neurogenesis . Contrary to popular belief, we continue to create brain cells throughout adulthood. By forcing ourselves to work on unfamiliar platforms at KPoz, we were opening our minds. When Rob wanted several football statistics downloads available on the Web site, we wrote a rake task to load the relevant data into the Google Spreadsheets API. An additional Rake task then updates a PHP page on the main sites with links to the new Spreadsheets. Since our system already contains multiple integrated platforms, adding another platform was simple. And notice how I say "we wrote". By including multiple development platforms on our site, we all learned more than we would have if it was a single platform solution.

At times while working on the site, I wondered if things would have been easier or more ideal if it was all written in Rails, or all PHP for that matter. However I've concluded that integrating multiple solutions is in many ways more ideal. This lead me to the concept of a Renaissance Programmer, a programmer that is versed in multiple platforms. I've also experienced yet another personal Programming Renaissance, in which I'm reminded that it's only the resulting product that matters.

Tuesday, January 6, 2009

The Recession Special

Yesterday we posted a Recession Special to Craigslist. The concept is, we have some talented developers who are looking for more work right now, and opportunities like this don't come along often. The last time there was a excess programming talent available was mid 2002. At the time , dice.com dropped from around 200,000 tech jobs to about 30,000. Work shortages for software developers usually don't last long. By 2003 the phone was ringing off the hook again. Just over a year ago , The number of tech jobs on Dice was well over 100,000. Today it says "Search 55,024 tech jobs", so it's clear there is a bit of a slowdown, though not like in 2002.


Dot com professionals from Austin's once thriving industry are gathered here at a Day Labor Assembly Point. Circa 2002.


In the Middle of Difficulty ...

... lies opportunity , as Einstein says. At AppTrain we're going to take on a couple projects at great rates right now, just to keep everyone busy. It will be interesting to see what types of companies are able to plan ahead financially. Last time the surprise was that more traditional conservative companies hired up talent at bargain rates. This time we're also hearing from Non-Profits that support the Arts who need technical help, but normally can't afford it. And it's always a pleasure to work on projects that support a worthy cause. So If you thought building a custom web application would be unaffordable, this could be your chance. For information, Contact AppTrain.

And as we say in the Craigslist ad:

*Offer only valid while the current quarterly US Gross Domestic Product (GDP) is lower than the previous quarter :) Act now while smart labor is still affordable!