Tuesday, December 04, 2007
Its Beging To Look A Lot Like Christmas
Monday, November 26, 2007
Office Remodel
I have been working on a housing listing site and just started to implement the advanced search feature. I found that I had not handled all the edge cases in my tests. Have no fear its up and working and of course I have some code to show you.
Here it is:
def self.search(params = {})
composed_cond = EZ::Where::Condition.new
cond_number_of_bedrooms = EZ::Where::Condition.new :properties do
number_of_bedrooms == params[:search][:number_of_bedrooms]
end
cond_min_price = EZ::Where::Condition.new :properties do
if !params[:search][:min_price].blank?
price >= params[:search][:min_price]
end
end
cond_max_price = EZ::Where::Condition.new :properties do
if !params[:search][:max_price].blank?
price <= params[:search][:max_price]
end
end
composed_cond << cond_number_of_bedrooms
composed_cond << cond_min_price
composed_cond << cond_max_price
@properties = Property.find(:all,
:include =>[:amenities, :utilities],
:conditions => composed_cond.to_sql)
end
-CH
Thursday, November 15, 2007
Elf List Goes Live
-CH
Monday, November 12, 2007
Godfather on Wii
-CH
Saturday, October 27, 2007
find or create by
if Product.find_by_name("spotted cow")
product = Product.find_by_name("spotted cow")
else
product = Product.create(:name => "spotted cow")
end
So I found out about this method from someone in the rails chat on irc.freenode.net (there is always someone willing to help in that chat room.)
Product.find_or_create_by_name( params[:product][:name],
:category_id => params[:product][:category_id],
:for => 'creation' )
So now the product will be either found or created by the name of the product and if it is going to be created it adds the category_id just like a normal create.
Here is the test that proves this works.
def test_find_or_create_by_name
assert Product.find_or_create_by_name("spotted cow",
:category_id => 1,
:for => :create)
assert Product.find_or_create_by_name("spotted cow",
:category_id => 1,
:for => :create)
assert Product.find_or_create_by_name("spotted cow",
:category_id => 1,
:for => :create)
assert Product.find_or_create_by_name("spotted cow",
:category_id => 1,
:for => :create)
products = Product.find_all_by_name("spotted cow")
assert_equal(1, products.length)
end
-CH
Friday, October 26, 2007
CVNUG (Chippewa Valley .Net User Group)
The presentation when off with out a hitch thanks to the help of Brian Hogan (http://www.napcs.com/) who was there to answer questions and explain information more in depth as I demonstrated building a "Cookbook" application. I stripped down a version of Brian's cookbook tutorial and turned it in to a demonstration.
I want to give a shout out to CVNUG (http://cvnug.wi-ineta.org/) for letting me present to them the wonders of working with Rails. I would also like to thank Brian for helping me out with the presentation.
-CH
Tuesday, October 23, 2007
Connecting to ms SQL from OS X
http://www.napcsweb.com/blog/2007/03/08/15/#more-15
Thursday, October 11, 2007
Rock N' Roll Jesus
Tuesday, October 02, 2007
Installing CS3 in OS X
http://www.adobe.com/support/contact/cs3clean.html
-CH
Monday, September 10, 2007
Ok Seriously I need to keep blogging
In other news I am almost finished with LauraSaidYes.net sometime next week I should have the site live. Its been such a crazy last couple of weeks with the start of school and the Rumble but now that everything has calmed down back to my crazy life I can concentrate on a million things rather than a million and one. :)
One more news worthy note, in October I will be presenting Ruby on Rails to the Chippewa Valley .Net User Group (http://cvnug.wi-ineta.org). This should be an exciting presentation opening some .Net developers to my world as they open me up to theirs every month.
-CH
Friday, August 31, 2007
Busy Busy
In other news I recently got engaged this past weekend. It really has been a blessing to have Laura in my life and her continuing support for me and everything I do.
Lastly I am presenting RoR to a group of .Net developers that I meet with every month. I will try and get my slidedeck up here as soon as I finish it for others to take a look at. Since I have been trying to learn .Net I think this will be a good communication point for me. I will say this, after programing in Ruby it makes it harder to want to give it up for another language. Because everything just makes sense.
Well stay tunned for more to come, as the fall semester is about to start I will inevitably find more information to share with the world.
-CH
Monday, August 13, 2007
Back to Search Results
So today I encountered a problem that I seem to encounter in every web application I develop. Most web applications require a search and a way to view each results details. When you view the details you most of the time want to go back to your search results and not have to re-enter all of the fields of your search form.
There are a few ways to do this.
First you can use the quick and dirty way that will not work all the time.
input value="Go" onclick="history.go(-1)" type="button"
The javascript function history.go is very useful if the client has javascript enabled. If they do not have javascript enabled this button will do absolutely nothing and frustrate your user beyond all means
The second option is to use the server to store the search params
# staff_controller.rb
def view_applications
if params[:type] == "requery"
search_params = session[:search_params]
else
search_params = params
session[:search_params] = params
end
@applications = Staff.find_applications_by_params(search_params)
end
As you can see when the view_applications method is called the params are checked for a variable called type, if this is being called from the original search that field would be empty and the else portion kicks in causing the params to be stored in session. Below you will see the link_to example from the page that shows the details of a record. You will notice that the params have a type that is set to requery, which triggers the function to use the params from session to display the results.
link_to "<< Back to Search Results", :action => :view_applications, :type => "requery"
There you have it, a way in rails to use the search results from before. If you have any other ways of doing this please post a comment.
Enjoy,
-CH
Wednesday, August 08, 2007
Monday, August 06, 2007
Wednesday, August 01, 2007
RoR Gotchas
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
Friday, July 27, 2007
Funny Friday
-CH
The following is an actual question given on a University of Washington
chemistry mid-term.
The answer by one student was so "profound" that the professor shared it
with colleagues, via the Internet, which is, of course, why we now have
the pleasure of enjoying it as well.
Bonus Question: Is Hell exothermic (gives off heat) or endothermic
(absorbs heat)?
Most of the students wrote proofs of their beliefs using Boyle's Law
(gas cools when it expands and heats when it is compressed) or some variant.
One student, however, wrote the following:
"First, we need to know how the mass of Hell is changing in time. So we
need to know the rate at which souls are moving into Hell and the rate
at which they are leaving. I think that we can safely assume that once a
soul gets to Hell, it will not leave. Therefore, no souls are leaving.
As for how many souls are entering Hell, let's look at the different
religions that exist in the world today. Most of these religions state
that if you are not a member of their religion, you will go to Hell.
Since there is more than one of these religions and since people do not
belong to more than one religion, we can project that all souls go to
Hell. With birth and death rates as they are, we can expect the number
of souls in Hell to increase exponentially.
Now, we look at the rate of change of the volume in Hell because Boyle's
Law states that in order for the temperature and pressure in Hell to stay
the same, the volume of Hell has to expand proportionately as souls are added.
This gives two possibilities:
1. If Hell is expanding at a slower rate than the rate at which souls
enter Hell, then the temperature and pressure in Hell will increase
until all Hell breaks loose.
2. If Hell is expanding at a rate faster than the increase of souls in
Hell, then the temperature and pressure will drop until Hell freezes
over.
So which is it? If we accept the postulate given to me by Teresa during
my Freshman year that, 'It will be a cold day in Hell before I sleep with
you,' and take into account the fact that I slept with her last night,
then number two must be true, and thus I am sure that Hell is exothermic
and has already frozen over.
The corollary of this theory is that since Hell has frozen over, it
follows that it is not accepting any more souls and is therefore,
extinct...leaving only Heaven, thereby proving the existence of a divine
being which explains why, last night, Teresa kept shouting 'Oh my God.'"
THIS STUDENT RECEIVED THE ONLY "A"
Wednesday, July 25, 2007
First 10 Minutes With IronRuby
After getting 'hello world' out there I wanted to start playing. First I wanted to display the current time out to the screen so I tried out the ruby command Time.now, no dice. I got a message saying "uninitialized constant Time", I thought Time.now was automatically initialized by system.
The next thing I tried that didn't work was using instance variables. I tried to use the statement: @msg = 'Hello World' and soon my console was filled with error messages. I was however able to use this statement: msg = 'Hello World'. I don't know if they plan on letting you use instance variables in IronRuby or not.
So over all I was not greatly impressed with IronRuby but am still excited for new versions to come. I guess 'Rome wasn't built in a day' and the progress the IronRuby team has made is in the right direction. Again I only played with this for 10 minuets thus am not an expert. So go out try it and let me know what you think.
-CH
Wednesday, June 27, 2007
Using ez_where
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:
There you have it, another example and my two cents.
def search_results
@alums = Alum.search(params)
render :action => "list"
end
Enjoy!
-CH