Wednesday, August 01, 2007

RoR Gotchas

So today I was working on re-factoring an application that I was handed. The application had everything plugged in the controller initially so in the name of MVC I started to move things to the model file. While in the model I had a method that looked like this:

def self.find_applications(params)
composed_cond =

cond_three_letter_code = :programs do
three_letter_code === params[:find_only_program_codes]

cond_status = :study_abroad_applications do
status === params[:find_only_statuses].
to_s.downcase.gsub(" ", "").gsub(",", ";").split(";")

cond_terms = :deadlines do
term_year === params[:find_only_terms].
to_s.downcase.gsub(" ", "").gsub(",", ";").split(";")

composed_cond << cond_three_letter_code
composed_cond << cond_terms
composed_cond << cond_status

@applications = StudyAbroadApplication.find(:all,
:include=>[{:deadline => :program}, :user],
:conditions => composed_cond.to_sql, :order => "")

unless @applications == 0
puts "Size: #{@applications.size}"
flash[:notice] = "There was an error retrieving your application records"
redirect_to :action => "index"

With me being a noob to RoR I completely forgot about my background in procedure based programing and thought nothing of the last statement. Hell if it puts out the size of the array thats what I wanted right? Well that was what I got, but what I didn't get was the @applications array that I wanted to display in my view. So there you have it just make sure that the last thing in your method is what you want to return. Learn from my mistakes and save your self some tail chasing. :)


No comments: