Wednesday, June 27, 2007

Using ez_where

While reading the documentation for the ez_where plugin in rails I got a little confused as I am a noob to rails. So once I figured out how to use this plugin I was able to develop tests and search functionality for an eight field search in under 5 hours. This was amazing to me coming from a classic asp background where this type of functionality would have taken a few days at least since some of the relationships would have required a very complex sql statement. But enough of my rambling lets get to the code.....

First off you'll want to get the ez_where plugin (http://opensvn.csie.org/ezra/rails/plugins/dev/ez_where/) inside the readme file the author provides several examples that are great and what I based my search off of. The nice part about this plugin is the way it extends active record. Rather then doing a Class.find(:all) you use Class.ez_find(:all) these will return the same results the fun part comes when you have two tables with related data that you want to search on. I'm going to use the authors examples and explain them a little more so thanks to Ezra Zygmuntowicz & Fabien Franzen for this great plugin.

First example:

articles = Article.ez_find(:all, :include => :author) do |article, author|
article.title =~ "%Foo Title%"
author.any do
name == 'Ezra'
name == 'Fab'
end
end

So first off we have the variable that we want to store the results in, in this case were looking for articles so were going to search using the main class of Article by implementing the ez_find method we are looking for all articles and also including the author association. This is what got me confused, as I stated I come from a procedural programming background using classic asp and the way I would have done this in asp would have been to write a humongous sql statement to search across the tables. So when I was reading the example I mistook the include => author for include the author table not the association that article. This was the biggest thing that I struggled with. So drop your associations in place and then build your logic for the search. I'm going to include my search as another example of how to implement this plugin.


def self.search(params)@alum = Alum.ez_find(:all,
:include => [:majors, :minors, :industry, :job_function])
do |alums, majors, minors, industry, job_function|
unless params[:search][:major].nil?
majors.id == params[:search][:major]
end

unless params[:search][:minor].nil?
minors.id == params[:search][:minor]
end

unless params[:search][:industry].nil?
industry.id == params[:search][:industry]
end

unless params[:search][:job_function].nil?
job_function.id == params[:search][:job_function]
end

unless params[:search][:employer].nil?
alums.employer == params[:search][:employer]
end

unless params[:search][:city].nil?
alums.city == params[:search][:city]
end

unless params[:search][:state].nil?
alums.state == params[:search][:state]
end
end
end


And now a test case example:

def test_search_for_employer_dimeo_family
alum = Alum.search(:search => {:employer => "DiMeo Family"})
assert_equal(3, alum.size)
assert alum.detect {|a| a.first_name == "Tony"}
assert alum.detect {|a| a.first_name == "Bobby"}
assert alum.detect {|a| a.first_name == "Christopher"}
end

And Finally the controller action:

def search_results
@alums = Alum.search(params)
render :action => "list"
end
There you have it, another example and my two cents.

Enjoy!
-CH

Monday, January 08, 2007

Night Time

Night time on the Milwaukee River.
Posted by Picasa

Sunday, December 31, 2006

Cedar Creek Park

Just playing around with long exposures and getting the blur in the running water.
Posted by Picasa

Tuesday, November 14, 2006

Water

Nothing to exciting just some water drops up really close. Posted by Picasa

Wednesday, October 18, 2006

One Leaf

This is just some simple photoshop magic, I really like the concept of just leaving on piece of the photo in color. Posted by Picasa

Friday, October 06, 2006

Night in Meno

Tonight I went out and played with some settings on my camera focusing in on motion and light. This stuff was shot using an iso100 and holding my shutter open for 1-2 seconds. It turned out pretty sweet in my opinion. Posted by Picasa

Friday, September 29, 2006

The Hills

This shot was taken just outside of Menomonie. I am not a super huge fan of it since the sun is distorting the top of the plant but for some reason this photograph intrigues me. I just don't know what it is. Posted by Picasa

Thursday, September 28, 2006

Storm Shots

Neither of these shots are perfect, I took this opportunity to try and capture the storm. The first one shows the layers of clouds with the long exposure of the ground. The second shot I'll admit was pure luck.


















Thursday, September 07, 2006

Ok Back again

I'm back at it, and hopefully everyone is super excited for this one. A nice senic shot on the beautiful Anvil Lake. Posted by Picasa

Monday, July 24, 2006

Mr. Bee

This is a shot I just happend to be walking by these flowers when I saw a bumble bee flying around and even in the flowers. A macro lens would have been perfect for this shot but as I am an amature photographer on a limited budget I make do with what I have! Posted by Picasa