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 = Caboose::EZ::Condition.new

cond_three_letter_code = Caboose::EZ::Condition.new :programs do
three_letter_code === params[:find_only_program_codes]
end

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

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

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 => "deadlines.id")

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


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. :)

-CH