Friends- I wanted to let people know that there is a new experimental release of this plugin. I would love feedback on syntax and features. There is now a full test suite with fixtures that covers all the available syntax. Look at the test suite for more syntax possibilities. There have been many additions since my last release. Fabien Atelier has been working on this with me and has added a bunch of great code. You can get it from here: http://http://brainspl.at/opensvn.csie.org/ezra/rails/plugins/dev/ ez_where/ This is a request for feedback. I want to solidify the api soon so suggestions are welcome. Here''s a snippet from the README: Welcome to the new improved ez_where plugin for rails. This plugin is meant to be used as a nice ruby like syntax for creating the :conditions part of an ActiveRecord::Base.find. We also add the ActiveRecord::Base.ez_find method. This method takes a block to simplify single and multi table queries. There is now support for nested sub conditions using a few different techniques. The default boolean used is ''AND''. any takes a subcondition block and uses ''OR'' to join them together. @articles = Article.ez_find(:all, :include => :author) do |article, author| article.title =~ "%Foo Title%" author.any do name == ''Ezra'' name == ''Fab'' end end This issue a find with these :conditions => ["article.title LIKE ? AND (authors.name = ? OR authors.name = ?)", "%Foo Title%", "Ezra", "Fab"] Basically here is the breakdown of how we map ruby operators to SQL operators: foo == ''bar'' #=> ["foo = ?", ''bar''] foo =~ ''%bar'' #=> ["foo LIKE ?", ''%bar''] foo <=> (1..5) #=> ["foo BETWEEN ? AND ?", 1, 5] id === [1, 2, 3, 5, 8] #=> ["id IN(?)", [1, 2, 3, 5, 8]] <, >, >=, <= et all will just work like you expect. And here is a link to my blog with the entire README for your perusal: http://brainspl.at/articles/2006/01/30/i-have-been-busy Cheers- -Ezra Zygmuntowicz WebMaster Yakima Herald-Republic Newspaper ezra@yakima-herald.com 509-577-7732 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060131/912175a1/attachment-0001.html
Nice idea Ezra! Will play with it when I can. b Ezra Zygmuntowicz wrote:> Friends- > > I wanted to let people know that there is a new experimental release of > this plugin. I would love feedback on syntax and features. There is now > a full test suite with fixtures that covers all the available syntax. > Look at the test suite for more syntax possibilities. There have been > many additions since my last release. Fabien Atelier has been working on > this with me and has added a bunch of great code. > > You can get it from here: > > http://http://brainspl.at/opensvn.csie.org/ezra/rails/plugins/dev/ez_where/ > > > This is a request for feedback. I want to solidify the api soon so > suggestions are welcome. > > Here''s a snippet from the README: > > Welcome to the new improved ez_where plugin for rails. This plugin is meant > to be used as a nice ruby like syntax for creating the :conditions part > of an > ActiveRecord::Base.find. We also add the ActiveRecord::Base.ez_find method. > This method takes a block to simplify single and multi table queries. There > is now support for nested sub conditions using a few different > techniques. The > default boolean used is ''AND''. any takes a subcondition block and uses ''OR'' > to join them together. > > @articles = Article.ez_find(:all, :include => :author) do |article, author| > article.title =~ "%Foo Title%" > author.any do > name == ''Ezra'' > name == ''Fab'' > end > end > > This issue a find with these :conditions => ["article.title LIKE ? AND > (authors.name = ? OR authors.name = ?)", "%Foo Title%", "Ezra", "Fab"] > > Basically here is the breakdown of how we map ruby operators to SQL > operators: > > foo == ''bar'' #=> ["foo = ?", ''bar''] > foo =~ ''%bar'' #=> ["foo LIKE ?", ''%bar''] > foo <=> (1..5) #=> ["foo BETWEEN ? AND ?", 1, 5] > id === [1, 2, 3, 5, 8] #=> ["id IN(?)", [1, 2, 3, 5, 8]] > <, >, >=, <= et all will just work like you expect. > > And here is a link to my blog with the entire README for your perusal: > > http://brainspl.at/articles/2006/01/30/i-have-been-busy > > Cheers- > -Ezra Zygmuntowicz > WebMaster > Yakima Herald-Republic Newspaper > ezra@yakima-herald.com <mailto:ezra@yakima-herald.com> > 509-577-7732 > > > ------------------------------------------------------------------------ > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Harvey Bernstein
2006-Jan-31 19:44 UTC
[Rails] Re: [ANN] ez_where plugin updated features.
This plugin seems to be exactly what I am looking for. However, I?ve been struggling this afternoon to get it to work. I should point out that I am a newbie to Rails. I?ve created a search page using a model which does not have a corresponding table, as outlined here (http://rails.techno-weenie.net/tip/2005/11/19/validate_your_forms_with_a_ta ble_less_model). This bit works perfectly as the simple validation rules work as expected. The problem I am having is building the where condition. I cannot seem to get the right syntax to get hold of the params value and build the condition. I?m just trying to build a very simple example. I have a textfield called postcode_area and all I want to do is create the following select (SELECT * FROM homes WHERE postcode LIKE ''%postcode_area%'' The code in the controller is as follows:- # Search for a home using the information entered in index. def search @home_search = HomeSearch.new(params[:home_search]) if @home_search.valid? @postcode_var = params[:home_search][:postcode_area] condition = Caboose::EZ::Condition.new do postcode =~ "%#{@postcode_var}%" # postcode =~ "%G77%" end @record_count = Home.count(condition.to_sql) if @record_count == 0 flash_message ''There are no results for this search. Please try again.'' redirect_to :action => ''index'' else @list_pages, @list_objects = paginate :homes, :per_page => 25, :conditions => condition.to_sql render :action => ''list'' end else render :action => ''index'' end end Everything in the table is returned because the SQL used is SELECT * FROM homes WHERE (postcode LIKE ''%%''). Here is the development log output:- Processing HomeController#search (for 127.0.0.1 at 2006-01-31 16:29:07) [POST] Parameters: {"home_search"=>{"postcode"=>"G77", "room_types"=>"", "care_types"=>"nursing care"}, "commit"=>"Search", "action"=>"search", "controller"=>"home", "max_price"=>"2000", "min_price"=>"0"} [4;36;1mHome Count (0.002739) [0;1mSELECT COUNT(*) FROM homes WHERE (postcode LIKE ''%%'') [4;35;1mHome Count (0.002695) SELECT COUNT(*) FROM homes WHERE (postcode LIKE ''%%'') [4;36;1mHome Load (0.003985) [0;1mSELECT * FROM homes WHERE (postcode LIKE ''%%'') LIMIT 0, 25 Rendering layoutfalseactionlist within layouts/application Rendering home/list Rendered shared/_list_left_side (0.00054) [4;35;1mHome Columns (0.001351) SHOW FIELDS FROM homes Rendered shared/_list_object (0.04215) Rendered shared/_list_object (0.00287) Rendered shared/_list_object (0.00250) Rendered shared/_list_object (0.00266) Rendered shared/_list_object (0.00250) Rendered shared/_list_object (0.00267) Rendered shared/_list_object (0.00823) Rendered shared/_list_object (0.00245) Rendered shared/_list_object (0.00292) Rendered shared/_list_object (0.00254) Rendered shared/_list_object (0.00229) Rendered shared/_list_object (0.00230) Rendered shared/_list_object (0.00248) Rendered shared/_list_object (0.00238) Rendered shared/_list_object (0.00232) Rendered shared/_list_object (0.00248) Rendered shared/_list_object (0.00256) Rendered shared/_list_object (0.00231) Rendered shared/_list_object (0.00247) Rendered shared/_list_object (0.00245) Rendered shared/_list_object (0.00232) Rendered shared/_list_object (0.00230) Rendered shared/_list_object (0.00255) Rendered shared/_list_object (0.00232) Rendered shared/_list_object (0.00232) Rendered shared/_list_right_side (0.13730) Rendered shared/_html_head (0.00587) Rendered shared/_top_menu (0.00289) Rendered shared/_agecare_menu (0.00548) Rendered shared/_footer (0.00178) Completed in 0.20923 (4 reqs/sec) | Rendering: 0.16196 (77%) | DB: 0.01077 (5%) | 200 OK [http://localhost/home/search] I know I am doing something really stupid but I have been going round in circles all afternoon. I would really appreciate any assistance. Regards Harvey On 31/1/06 03:32, "Ezra Zygmuntowicz" <ezra@yakimaherald.com> wrote:> Friends- > > I wanted to let people know that there is a new experimental release > of this plugin. I would love feedback on syntax and features. There is > now a full test suite with fixtures that covers all the available > syntax. Look at the test suite for more syntax possibilities. There > have been many additions since my last release. Fabien Atelier has > been working on this with me and has added a bunch of great code. > > You can get it from here: > > > http://http://brainspl.at/opensvn.csie.org/ezra/rails/plugins/dev/ez_w > here/ > > > This is a request for feedback. I want to solidify the api soon so > suggestions are welcome. > > Here''s a snippet from the README: > > Welcome to the new improved ez_where plugin for rails. This plugin is > meant to be used as a nice ruby like syntax for creating the > :conditions part of an ActiveRecord::Base.find. We also add the ActiveRecord::Base.ez_find method. > This method takes a block to simplify single and multi table queries. > There is now support for nested sub conditions using a few different > techniques. The default boolean used is ''AND''. any takes a subcondition block and uses ''OR'' > to join them together. > > @articles = Article.ez_find(:all, :include => :author) do |article, author| > article.title =~ "%Foo Title%" > author.any do > name == ''Ezra'' > name == ''Fab'' > end > end > > This issue a find with these :conditions => ["article.title LIKE ? AND > (authors.name = ? OR authors.name = ?)", "%Foo Title%", "Ezra", "Fab"] > > Basically here is the breakdown of how we map ruby operators to SQL operators: > > foo == ''bar'' #=> ["foo = ?", ''bar''] > foo =~ ''%bar'' #=> ["foo LIKE ?", ''%bar''] > foo <=> (1..5) #=> ["foo BETWEEN ? AND ?", 1, 5] > id === [1, 2, 3, 5, 8] #=> ["id IN(?)", [1, 2, 3, 5, 8]] <, >, >=, <= > et all will just work like you expect. > > And here is a link to my blog with the entire README for your perusal: > > http://brainspl.at/articles/2006/01/30/i-have-been-busy > > Cheers- > > -Ezra Zygmuntowicz > WebMaster > Yakima Herald-Republic Newspaper > ezra@yakima-herald.com > 509-577-7732 > > > > This e-mail has been scanned for all viruses by MessageLabs. > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/railsEzra Zygmuntowicz wrote:> Friends- > > I wanted to let people know that there is a new experimental release > of this plugin. I would love feedback on syntax and features. There > is now a full test suite with fixtures that covers all the available > syntax. Look at the test suite for more syntax possibilities. There > have been many additions since my last release. Fabien Atelier has > been working on this with me and has added a bunch of great code. > > You can get it from here: > > http://http://brainspl.at/opensvn.csie.org/ezra/rails/plugins/dev/ > ez_where/ > > This is a request for feedback. I want to solidify the api soon so > suggestions are welcome. > > Here''s a snippet from the README: > > Welcome to the new improved ez_where plugin for rails. This plugin is > meant > to be used as a nice ruby like syntax for creating the :conditions > part of an > ActiveRecord::Base.find. We also add the ActiveRecord::Base.ez_find > method. > This method takes a block to simplify single and multi table queries. > There > is now support for nested sub conditions using a few different > techniques. The > default boolean used is ''AND''. any takes a subcondition block and > uses ''OR'' > to join them together. > > @articles = Article.ez_find(:all, :include => :author) do |article, > author| > article.title =~ "%Foo Title%" > author.any do > name == ''Ezra'' > name == ''Fab'' > end > end > > This issue a find with these :conditions => ["article.title LIKE ? AND > (authors.name = ? OR authors.name = ?)", "%Foo Title%", "Ezra", "Fab"] > > Basically here is the breakdown of how we map ruby operators to SQL > operators: > > foo == ''bar'' #=> ["foo = ?", ''bar''] > foo =~ ''%bar'' #=> ["foo LIKE ?", ''%bar''] > foo <=> (1..5) #=> ["foo BETWEEN ? AND ?", 1, 5] > id === [1, 2, 3, 5, 8] #=> ["id IN(?)", [1, 2, 3, 5, 8]] > <, >, >=, <= et all will just work like you expect. > > And here is a link to my blog with the entire README for your perusal: > > http://brainspl.at/articles/2006/01/30/i-have-been-busy > > Cheers- > -Ezra Zygmuntowicz > WebMaster > Yakima Herald-Republic Newspaper > ezra@yakima-herald.com > 509-577-7732-- Posted via http://www.ruby-forum.com/.
Ezra Zygmuntowicz
2006-Jan-31 21:03 UTC
[Rails] Re: [ANN] ez_where plugin updated features.
Harvey- Thanks for testing this out for me. It will be easy to do what you want. The problem you are having relates to the use of instance vars like @postcode_var. Since the condition is getting evaluated inside the Caboose::EZ::Condition block, @postcode_var won''t work and will return the sting you wanted as ''%%''. The way around this is to just use plain old local vars. SO what you want to do can be done like this: postcode_var = params[:home_search][:postcode_area] cond = Caboose::EZ::Condition.new do postcode =~ "%#{postcode_var}%" end @record_count = Home.count(cond.to_sql) Also you could do the same thing like this: cond = Home.ez_condition { postcode =~ "%#{postcode_var}%" } @record_count = Home.count(cond.to_sql) I need to add to the docs the fact that you can''t use @instance vars inside the conditions as they are not evaluated in the correct context. Try this solution out and see how it works for you and let me know. Cheers- -Ezra On Jan 31, 2006, at 11:41 AM, Harvey Bernstein wrote:> This plugin seems to be exactly what I am looking for. However, I?ve > been struggling this afternoon to get it to work. I should point out > that I am a newbie to Rails. I?ve created a search page using a model > which does not have a corresponding table, as outlined here > (http://rails.techno-weenie.net/tip/2005/11/19/ > validate_your_forms_with_a_ta > ble_less_model). This bit works perfectly as the simple validation > rules > work as expected. > > The problem I am having is building the where condition. I cannot > seem > to get the right syntax to get hold of the params value and build the > condition. I?m just trying to build a very simple example. I have a > textfield called postcode_area and all I want to do is create the > following select (SELECT * FROM homes WHERE postcode LIKE > ''%postcode_area%'' > > > The code in the controller is as follows:- > > # Search for a home using the information entered in index. > def search > @home_search = HomeSearch.new(params[:home_search]) > > if @home_search.valid? > @postcode_var = params[:home_search][:postcode_area] > > condition = Caboose::EZ::Condition.new do > postcode =~ "%#{@postcode_var}%" > # postcode =~ "%G77%" > end > > @record_count = Home.count(condition.to_sql) > if @record_count == 0 > flash_message ''There are no results for this search. > Please try > again.'' > redirect_to :action => ''index'' > else > @list_pages, @list_objects = paginate :homes, :per_page => 25, > :conditions => condition.to_sql > render :action => ''list'' > end > else > render :action => ''index'' > end > end > > Everything in the table is returned because the SQL used is SELECT * > FROM homes WHERE (postcode LIKE ''%%''). Here is the development log > output:- > > > Processing HomeController#search (for 127.0.0.1 at 2006-01-31 > 16:29:07) > [POST] > Parameters: {"home_search"=>{"postcode"=>"G77", "room_types"=>"", > "care_types"=>"nursing care"}, "commit"=>"Search", "action"=>"search", > "controller"=>"home", "max_price"=>"2000", "min_price"=>"0"} > [4;36;1mHome Count (0.002739) [0;1mSELECT COUNT(*) FROM > homes > WHERE (postcode LIKE ''%%'') > [4;35;1mHome Count (0.002695) SELECT COUNT(*) FROM homes > WHERE > (postcode LIKE ''%%'') > [4;36;1mHome Load (0.003985) [0;1mSELECT * FROM homes WHERE > (postcode LIKE ''%%'') LIMIT 0, 25 > Rendering layoutfalseactionlist within layouts/application Rendering > home/list Rendered shared/_list_left_side (0.00054) > [4;35;1mHome Columns (0.001351) SHOW FIELDS FROM homes > [0m > Rendered shared/_list_object (0.04215) > Rendered shared/_list_object (0.00287) > Rendered shared/_list_object (0.00250) > Rendered shared/_list_object (0.00266) > Rendered shared/_list_object (0.00250) > Rendered shared/_list_object (0.00267) > Rendered shared/_list_object (0.00823) > Rendered shared/_list_object (0.00245) > Rendered shared/_list_object (0.00292) > Rendered shared/_list_object (0.00254) > Rendered shared/_list_object (0.00229) > Rendered shared/_list_object (0.00230) > Rendered shared/_list_object (0.00248) > Rendered shared/_list_object (0.00238) > Rendered shared/_list_object (0.00232) > Rendered shared/_list_object (0.00248) > Rendered shared/_list_object (0.00256) > Rendered shared/_list_object (0.00231) > Rendered shared/_list_object (0.00247) > Rendered shared/_list_object (0.00245) > Rendered shared/_list_object (0.00232) > Rendered shared/_list_object (0.00230) > Rendered shared/_list_object (0.00255) > Rendered shared/_list_object (0.00232) > Rendered shared/_list_object (0.00232) > Rendered shared/_list_right_side (0.13730) Rendered shared/_html_head > (0.00587) Rendered shared/_top_menu (0.00289) Rendered > shared/_agecare_menu (0.00548) Rendered shared/_footer (0.00178) > Completed in 0.20923 (4 reqs/sec) | Rendering: 0.16196 (77%) | DB: > 0.01077 > (5%) | 200 OK [http://localhost/home/search] > > > I know I am doing something really stupid but I have been going > round in > circles all afternoon. I would really appreciate any assistance. > Regards > Harvey > > > > On 31/1/06 03:32, "Ezra Zygmuntowicz" <ezra@yakimaherald.com> wrote: > >> Friends- >> >> I wanted to let people know that there is a new experimental release >> of this plugin. I would love feedback on syntax and features. >> There is >> now a full test suite with fixtures that covers all the available >> syntax. Look at the test suite for more syntax possibilities. There >> have been many additions since my last release. Fabien Atelier has >> been working on this with me and has added a bunch of great code. >> >> You can get it from here: >> >> >> http://http://brainspl.at/opensvn.csie.org/ezra/rails/plugins/dev/ >> ez_w >> here/ >> >> >> This is a request for feedback. I want to solidify the api soon so >> suggestions are welcome. >> >> Here''s a snippet from the README: >> >> Welcome to the new improved ez_where plugin for rails. This plugin is >> meant to be used as a nice ruby like syntax for creating the >> :conditions part of an ActiveRecord::Base.find. We also add the >> ActiveRecord::Base.ez_find method. >> This method takes a block to simplify single and multi table queries. >> There is now support for nested sub conditions using a few different >> techniques. The default boolean used is ''AND''. any takes a >> subcondition block and uses ''OR'' >> to join them together. >> >> @articles = Article.ez_find(:all, :include => :author) do | >> article, author| >> article.title =~ "%Foo Title%" >> author.any do >> name == ''Ezra'' >> name == ''Fab'' >> end >> end >> >> This issue a find with these :conditions => ["article.title LIKE ? >> AND >> (authors.name = ? OR authors.name = ?)", "%Foo Title%", "Ezra", >> "Fab"] >> >> Basically here is the breakdown of how we map ruby operators to >> SQL operators: >> >> foo == ''bar'' #=> ["foo = ?", ''bar''] >> foo =~ ''%bar'' #=> ["foo LIKE ?", ''%bar''] >> foo <=> (1..5) #=> ["foo BETWEEN ? AND ?", 1, 5] >> id === [1, 2, 3, 5, 8] #=> ["id IN(?)", [1, 2, 3, 5, 8]] <, >, >=, <>> et all will just work like you expect. >> >> And here is a link to my blog with the entire README for your >> perusal: >> >> http://brainspl.at/articles/2006/01/30/i-have-been-busy >> >> Cheers- >> >> -Ezra Zygmuntowicz >> WebMaster >> Yakima Herald-Republic Newspaper >> ezra@yakima-herald.com >> 509-577-7732 >> >> >> >> This e-mail has been scanned for all viruses by MessageLabs. >> >> >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > > Ezra Zygmuntowicz wrote: >> Friends- >> >> I wanted to let people know that there is a new experimental release >> of this plugin. I would love feedback on syntax and features. There >> is now a full test suite with fixtures that covers all the available >> syntax. Look at the test suite for more syntax possibilities. There >> have been many additions since my last release. Fabien Atelier has >> been working on this with me and has added a bunch of great code. >> >> You can get it from here: >> >> http://http://brainspl.at/opensvn.csie.org/ezra/rails/plugins/dev/ >> ez_where/ >> >> This is a request for feedback. I want to solidify the api soon so >> suggestions are welcome. >> >> Here''s a snippet from the README: >> >> Welcome to the new improved ez_where plugin for rails. This plugin is >> meant >> to be used as a nice ruby like syntax for creating the :conditions >> part of an >> ActiveRecord::Base.find. We also add the ActiveRecord::Base.ez_find >> method. >> This method takes a block to simplify single and multi table queries. >> There >> is now support for nested sub conditions using a few different >> techniques. The >> default boolean used is ''AND''. any takes a subcondition block and >> uses ''OR'' >> to join them together. >> >> @articles = Article.ez_find(:all, :include => :author) do |article, >> author| >> article.title =~ "%Foo Title%" >> author.any do >> name == ''Ezra'' >> name == ''Fab'' >> end >> end >> >> This issue a find with these :conditions => ["article.title LIKE ? >> AND >> (authors.name = ? OR authors.name = ?)", "%Foo Title%", "Ezra", >> "Fab"] >> >> Basically here is the breakdown of how we map ruby operators to SQL >> operators: >> >> foo == ''bar'' #=> ["foo = ?", ''bar''] >> foo =~ ''%bar'' #=> ["foo LIKE ?", ''%bar''] >> foo <=> (1..5) #=> ["foo BETWEEN ? AND ?", 1, 5] >> id === [1, 2, 3, 5, 8] #=> ["id IN(?)", [1, 2, 3, 5, 8]] >> <, >, >=, <= et all will just work like you expect. >> >> And here is a link to my blog with the entire README for your >> perusal: >> >> http://brainspl.at/articles/2006/01/30/i-have-been-busy >> >> Cheers- >> -Ezra Zygmuntowicz >> WebMaster >> Yakima Herald-Republic Newspaper >> ezra@yakima-herald.com >> 509-577-7732 > > > -- > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-Ezra Zygmuntowicz Yakima Herald-Republic WebMaster http://yakimaherald.com 509-577-7732 ezra@yakima-herald.com
Harvey Bernstein
2006-Feb-02 15:04 UTC
[Rails] Re: [ANN] ez_where plugin updated features.
Ezra Thanks for all your help, its very much appreciated. I made the changes you suggested but I am still having the same problem. The SQL comes out as SELECT COUNT(*) FROM homes WHERE (postcode LIKE ''%%''). The good news is that I have managed to add a select drop down. The select is called care_type and contains two options, namely "nursing care" and "residential care". This works great giving me the correct SQL Here is the code:- Model=============================== class HomeSearch < ActiveRecord::Base def self.columns() @columns ||= []; end def self.column(name, sql_type = nil, default = nil, null = true) columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null) end column :care_type, :string column :postcode, :string column :room_type, :string column :min_price, :string column :max_price, :string validates_presence_of :care_type, :postcode #todo validation for min, max etc. end View - index rhtml search form============================ <div id="right_side"> <div id="right_side_border"> <%= error_messages_for ''home_search'', { :header => ''Errors prohibited this home search from being processed''} %> <%= start_form_tag :action => ''search'' %> <b><label for="home_search_care_type">I am looking for a home that provides </label></b> <%= select ''home_search'', ''care_type'', care_types, {:prompt=>"please select"} %> <p><b><label for="home_search_postcode">The home should be in the </label> <%= text_field ''home_search'', ''postcode'', :maxlength => 5 %> postcode area </b></p> <div id="extra_options_for_home_search" style="display:none;"> <p>The room should be <%= select ''home_search'', ''room_type'', room_types, {:prompt => "please select" } %></p> <p>The room should cost between ? <%= text_field ''home_search'', ''min_price'' %> and <%= text_field ''home_search'', ''max_price'' %> per week </p> </div> <div id="search_options"> <%= link_to_function "Advanced Search Options", "Effect.BlindDown(''extra_options_for_home_search'');" %> <%= submit_tag "Search" %> </div> <%= end_form_tag %> </div> </div> Controller ====================================================== # Display the home search page. def index @home_search = HomeSearch.new end # Search for a home using the information entered in index. def search @home_search = HomeSearch.new(params[:home_search]) if @home_search.valid? postcode_var = params[:home_search][:postcode_area] care_type_var = params[:home_search][:care_type] room_type_var = params[:home_search][:room_type] min_price_var = params[:home_search][:min_price] max_price_var = params[:home_search][:max_price] cond = Caboose::EZ::Condition.new do postcode =~ "%#{postcode_var}%" if care_type_var == "nursing care" nursing == true # single_nursing_charge <=> (min_price_var..max_price_var) elsif care_type_var == "residential care" residential == true end end @record_count = Home.count(cond.to_sql) if @record_count == 0 flash_message ''There are no results for this search. Please try again.'' redirect_to :action => ''index'' else @list_pages, @list_objects = paginate :homes, :per_page => 25, :conditions => cond.to_sql render :action => ''list'' end else render :action => ''index'' end end Log output =================================== Processing HomeController#search (for 127.0.0.1 at 2006-02-02 12:59:28) [POST] Parameters: {"home_search"=>{"room_type"=>"", "postcode"=>"G77", "max_price"=>"", "care_type"=>"nursing care", "min_price"=>""}, "commit"=>"Search", "action"=>"search", "controller"=>"home"} required_perm is home/search [4;35;1mRole Load (0.001120) SELECT * FROM roles LEFT JOIN users_roles ON roles.id = users_roles.role_id WHERE (users_roles.user_id = 5 ) [4;36;1mHome Count (0.003248) [0;1mSELECT COUNT(*) FROM homes WHERE (postcode LIKE ''%%'' AND nursing = 1) [4;35;1mHome Count (0.003192) SELECT COUNT(*) FROM homes WHERE (postcode LIKE ''%%'' AND nursing = 1) [4;36;1mHome Load (0.031579) [0;1mSELECT * FROM homes WHERE (postcode LIKE ''%%'' AND nursing = 1) LIMIT 0, 25 Rendering layoutfalseactionlist within layouts/application Rendering home/list Rendered shared/_list_left_side (0.00074) [4;35;1mHome Columns (0.003625) SHOW FIELDS FROM homes Rendered shared/_list_object (0.04139) Rendered shared/_list_object (0.00252) Rendered shared/_list_object (0.01578) Rendered shared/_list_right_side (0.06390) Rendered shared/_html_head (0.00524) Rendered shared/_top_menu (0.00303) Rendered shared/_agecare_menu (0.00592) Rendered shared/_footer (0.00199) Completed in 0.14636 (6 reqs/sec) | Rendering: 0.08243 (56%) | DB: 0.04331 (29%) | 200 OK [http://localhost/home/search] Sorry about the length of this email. Any help would be much appreciated. Regards Harvey On 31/1/06 21:00, "Ezra Zygmuntowicz" <ezra@yakima-herald.com> wrote: > Harvey- This e-mail has been scanned for all viruses by MessageLabs.
Hi Harvey, I''m one of the co-devs for the ez_where plugin. I don''t see why your code wouldn''t work, unless offcoarse, postcode_var is actually empty. Did you try a hardcoded value for postcode_var? Regards, Fabien
Harvey Bernstein
2006-Feb-02 16:17 UTC
[Rails] Re: [ANN] ez_where plugin updated features.
Thanks I feel so stupid! I''ve been looking at this all day and I didn''t release that I had postcode_var = params[:home_search][:postcode_area] instead of postcode_var = params[:home_search][:postcode] I''ll go and crawl under a rock. Thank you Harvey ------ Forwarded Message From: Fabien <fabienf@gmail.com> Reply-To: <rails@lists.rubyonrails.org> Date: Thu, 2 Feb 2006 16:03:27 +0000 (UTC) To: <rails@lists.rubyonrails.org> Subject: [Rails] Re: [ANN] ez_where plugin updated features. Hi Harvey, I''m one of the co-devs for the ez_where plugin. I don''t see why your code wouldn''t work, unless offcoarse, postcode_var is actually empty. Did you try a hardcoded value for postcode_var? Regards, Fabien _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails This e-mail has been scanned for all viruses by MessageLabs. ------ End of Forwarded Message This e-mail has been scanned for all viruses by MessageLabs.
Harvey Bernstein
2006-Feb-02 16:50 UTC
[Rails] Re: [ANN] ez_where plugin updated features.
Guys really loving your work. All appears to be working as advertised but I will be doing more testing tomorrow. One more question I hope you can help me with. My condition is becoming a rather long if/else statement. I was just wondering if there is a more ruby like way of handling this situation:- cond = Caboose::EZ::Condition.new do postcode =~ "%#{postcode_var}%" if care_type_var == "nursing care" nursing == true if room_type_var == "single" single_nursing_charge <=> (min_price_var..max_price_var) single > 0 elsif room_type_var == "single with ensuite" single_ensuite_nursing_charge <=> (min_price_var..max_price_var) single_ensuite > 0 elsif room_type_var == "twin" twin_nursing_charge <=> (min_price_var..max_price_var) twin > 0 elsif room_type_var == "twin with ensuite" twin_ensuite_nursing_charge <=> (min_price_var..max_price_var) twin_ensuite > 0 elsif room_type_var == "multi" multi_nursing_charge <=> (min_price_var..max_price_var) multi > 0 elsif room_type_var == "multi with ensuite" multi_ensuite_nursing_charge <=> (min_price_var..max_price_var) multi_ensuite > 0 end elsif care_type_var == "residential care" residential == true if room_type_var == "single" single_residential_charge <=> (min_price_var..max_price_var) single > 0 elsif room_type_var == "single with ensuite" single_ensuite_residential_charge <=> (min_price_var..max_price_var) single_ensuite > 0 elsif room_type_var == "twin" twin_residential_charge <=> (min_price_var..max_price_var) twin > 0 elsif room_type_var == "twin with ensuite" twin_ensuite_residential_charge <=> (min_price_var..max_price_var) twin_ensuite > 0 elsif room_type_var == "multi" multi_residential_charge <=> (min_price_var..max_price_var) multi > 0 elsif room_type_var == "multi with ensuite" multi_ensuite_residential_charge <=> (min_price_var..max_price_var) multi_ensuite > 0 end end end Thanks in advance. Harvey This e-mail has been scanned for all viruses by MessageLabs.
Glad you like it. Instead of using if...elsif...elsif... you could use a ''case'' structure. Also, if you feel your condition becomes rather longwinded, you can always break the condition into multiple parts you then append them together; something like: cond = Caboose::EZ::Condition.new cond.postcode =~ "%#postcode_var}" if care_type_var == ''nursing care'' case room_type_var when ''single'': ... when ''single with ensuite'' ... end else ... end
Ezra Zygmuntowicz
2006-Feb-02 23:05 UTC
[Rails] Re: [ANN] ez_where plugin updated features.
Harvey- Woah dude that is a big looking query ;-) Yeah like Fabien said I would split it up a bit and use case statements. I''m glad you like the plugin. Keep pushing it hard and try to break it ;-) Then we can pick up the pieces and make i better. Cheers- -Ezra On Feb 2, 2006, at 8:50 AM, Harvey Bernstein wrote:> Guys really loving your work. All appears to be working as > advertised but I > will be doing more testing tomorrow. One more question I hope you > can help > me with. My condition is becoming a rather long if/else > statement. I was > just wondering if there is a more ruby like way of handling this > situation:- > > cond = Caboose::EZ::Condition.new do > postcode =~ "%#{postcode_var}%" > > if care_type_var == "nursing care" > nursing == true > if room_type_var == "single" > single_nursing_charge <=> > (min_price_var..max_price_var) > single > 0 > elsif room_type_var == "single with ensuite" > single_ensuite_nursing_charge <=> > (min_price_var..max_price_var) > single_ensuite > 0 > elsif room_type_var == "twin" > twin_nursing_charge <=> (min_price_var..max_price_var) > twin > 0 > elsif room_type_var == "twin with ensuite" > twin_ensuite_nursing_charge <=> > (min_price_var..max_price_var) > twin_ensuite > 0 > elsif room_type_var == "multi" > multi_nursing_charge <=> > (min_price_var..max_price_var) > multi > 0 > elsif room_type_var == "multi with ensuite" > multi_ensuite_nursing_charge <=> > (min_price_var..max_price_var) > multi_ensuite > 0 > end > > elsif care_type_var == "residential care" > residential == true > if room_type_var == "single" > single_residential_charge <=> > (min_price_var..max_price_var) > single > 0 > elsif room_type_var == "single with ensuite" > single_ensuite_residential_charge <=> > (min_price_var..max_price_var) > single_ensuite > 0 > elsif room_type_var == "twin" > twin_residential_charge <=> > (min_price_var..max_price_var) > twin > 0 > elsif room_type_var == "twin with ensuite" > twin_ensuite_residential_charge <=> > (min_price_var..max_price_var) > twin_ensuite > 0 > elsif room_type_var == "multi" > multi_residential_charge <=> > (min_price_var..max_price_var) > multi > 0 > elsif room_type_var == "multi with ensuite" > multi_ensuite_residential_charge <=> > (min_price_var..max_price_var) > multi_ensuite > 0 > end > end > end > > Thanks in advance. > Harvey > > > This e-mail has been scanned for all viruses by MessageLabs. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-Ezra Zygmuntowicz Yakima Herald-Republic WebMaster http://yakimaherald.com 509-577-7732 ezra@yakima-herald.com
Hey Harvey, I''m new to Ruby myself and still looking for good idiomatic programming tips. I don''t have half the Rails knowledge of Ezra and Fabien, but I can make a recommendation. This comes from a great book by Martin Fowler called "Refactoring." I forgot the exact title but it''s something like "Transform Nested Conditionals to Polymorphism." The idea basically is that any time you''ve got an action which is executed differently depending on some condition, you''ve got polymorphism. So you can make the whole thing much less effort, and much much nicer to read, by taking out that room type var and instead creating a Room object which knows its own type. Likewise, instead of having two variables to track whether or not there''s nursing going on (nursing = true / care_type_var == "nursing care"), you just have a Guest var which knows whether or not the guest requires any kind of nursing. (If I didn''t suspect you were doing something connected to the care of senior citizens I would say all this talk of nursing sounds a bit kinky.) Anyway, you end up with pretty much the same amount of code, but instead of a long cascade of if/else statements, you encapsulate all that stuff in objects, and it''s much easier to read it that way. The stuff about residential/nursing goes in the Guest object, the stuff about single/double goes in the Room object, and then you just have a Charge object which gets sent to the Guest and the Room to be properly added up or whatever. Hope this helps... Giles On 2/2/06, Ezra Zygmuntowicz <ezra@yakima-herald.com> wrote:> Harvey- > > Woah dude that is a big looking query ;-) Yeah like Fabien said I > would split it up a bit and use case statements. I''m glad you like > the plugin. Keep pushing it hard and try to break it ;-) Then we can > pick up the pieces and make i better. > > Cheers- > -Ezra > > On Feb 2, 2006, at 8:50 AM, Harvey Bernstein wrote: > > > Guys really loving your work. All appears to be working as > > advertised but I > > will be doing more testing tomorrow. One more question I hope you > > can help > > me with. My condition is becoming a rather long if/else > > statement. I was > > just wondering if there is a more ruby like way of handling this > > situation:- > > > > cond = Caboose::EZ::Condition.new do > > postcode =~ "%#{postcode_var}%" > > > > if care_type_var == "nursing care" > > nursing == true > > if room_type_var == "single" > > single_nursing_charge <=> > > (min_price_var..max_price_var) > > single > 0 > > elsif room_type_var == "single with ensuite" > > single_ensuite_nursing_charge <=> > > (min_price_var..max_price_var) > > single_ensuite > 0 > > elsif room_type_var == "twin" > > twin_nursing_charge <=> (min_price_var..max_price_var) > > twin > 0 > > elsif room_type_var == "twin with ensuite" > > twin_ensuite_nursing_charge <=> > > (min_price_var..max_price_var) > > twin_ensuite > 0 > > elsif room_type_var == "multi" > > multi_nursing_charge <=> > > (min_price_var..max_price_var) > > multi > 0 > > elsif room_type_var == "multi with ensuite" > > multi_ensuite_nursing_charge <=> > > (min_price_var..max_price_var) > > multi_ensuite > 0 > > end > > > > elsif care_type_var == "residential care" > > residential == true > > if room_type_var == "single" > > single_residential_charge <=> > > (min_price_var..max_price_var) > > single > 0 > > elsif room_type_var == "single with ensuite" > > single_ensuite_residential_charge <=> > > (min_price_var..max_price_var) > > single_ensuite > 0 > > elsif room_type_var == "twin" > > twin_residential_charge <=> > > (min_price_var..max_price_var) > > twin > 0 > > elsif room_type_var == "twin with ensuite" > > twin_ensuite_residential_charge <=> > > (min_price_var..max_price_var) > > twin_ensuite > 0 > > elsif room_type_var == "multi" > > multi_residential_charge <=> > > (min_price_var..max_price_var) > > multi > 0 > > elsif room_type_var == "multi with ensuite" > > multi_ensuite_residential_charge <=> > > (min_price_var..max_price_var) > > multi_ensuite > 0 > > end > > end > > end > > > > Thanks in advance. > > Harvey > > > > > > This e-mail has been scanned for all viruses by MessageLabs. > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > -Ezra Zygmuntowicz > Yakima Herald-Republic > WebMaster > http://yakimaherald.com > 509-577-7732 > ezra@yakima-herald.com > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Giles Goat Boy http://gilesmakesmusic.blogspot.com http://gileswritescode.blogspot.com
Harvey Bernstein
2006-Feb-07 16:05 UTC
[Rails] Re: [ANN] ez_where plugin updated features.
Giles Thanks for the advice. Something just did not feel right. I''ll give it a go and hopeful it will be easier to maintain later on Thanks Harvey On 6/2/06 23:44, "Giles Bowkett" <gilesb@gmail.com> wrote:> Hey Harvey, I''m new to Ruby myself and still looking for good > idiomatic programming tips. I don''t have half the Rails knowledge of > Ezra and Fabien, but I can make a recommendation. This comes from a > great book by Martin Fowler called "Refactoring." I forgot the exact > title but it''s something like "Transform Nested Conditionals to > Polymorphism." > > The idea basically is that any time you''ve got an action which is > executed differently depending on some condition, you''ve got > polymorphism. So you can make the whole thing much less effort, and > much much nicer to read, by taking out that room type var and instead > creating a Room object which knows its own type. Likewise, instead of > having two variables to track whether or not there''s nursing going on > (nursing = true / care_type_var == "nursing care"), you just have a > Guest var which knows whether or not the guest requires any kind of > nursing. > > (If I didn''t suspect you were doing something connected to the care of > senior citizens I would say all this talk of nursing sounds a bit > kinky.) > > Anyway, you end up with pretty much the same amount of code, but > instead of a long cascade of if/else statements, you encapsulate all > that stuff in objects, and it''s much easier to read it that way. The > stuff about residential/nursing goes in the Guest object, the stuff > about single/double goes in the Room object, and then you just have a > Charge object which gets sent to the Guest and the Room to be properly > added up or whatever. > > Hope this helps... > Giles > > On 2/2/06, Ezra Zygmuntowicz <ezra@yakima-herald.com> wrote: >> Harvey- >> >> Woah dude that is a big looking query ;-) Yeah like Fabien said I >> would split it up a bit and use case statements. I''m glad you like >> the plugin. Keep pushing it hard and try to break it ;-) Then we can >> pick up the pieces and make i better. >> >> Cheers- >> -Ezra >> >> On Feb 2, 2006, at 8:50 AM, Harvey Bernstein wrote: >> >>> Guys really loving your work. All appears to be working as >>> advertised but I >>> will be doing more testing tomorrow. One more question I hope you >>> can help >>> me with. My condition is becoming a rather long if/else >>> statement. I was >>> just wondering if there is a more ruby like way of handling this >>> situation:- >>> >>> cond = Caboose::EZ::Condition.new do >>> postcode =~ "%#{postcode_var}%" >>> >>> if care_type_var == "nursing care" >>> nursing == true >>> if room_type_var == "single" >>> single_nursing_charge <=> >>> (min_price_var..max_price_var) >>> single > 0 >>> elsif room_type_var == "single with ensuite" >>> single_ensuite_nursing_charge <=> >>> (min_price_var..max_price_var) >>> single_ensuite > 0 >>> elsif room_type_var == "twin" >>> twin_nursing_charge <=> (min_price_var..max_price_var) >>> twin > 0 >>> elsif room_type_var == "twin with ensuite" >>> twin_ensuite_nursing_charge <=> >>> (min_price_var..max_price_var) >>> twin_ensuite > 0 >>> elsif room_type_var == "multi" >>> multi_nursing_charge <=> >>> (min_price_var..max_price_var) >>> multi > 0 >>> elsif room_type_var == "multi with ensuite" >>> multi_ensuite_nursing_charge <=> >>> (min_price_var..max_price_var) >>> multi_ensuite > 0 >>> end >>> >>> elsif care_type_var == "residential care" >>> residential == true >>> if room_type_var == "single" >>> single_residential_charge <=> >>> (min_price_var..max_price_var) >>> single > 0 >>> elsif room_type_var == "single with ensuite" >>> single_ensuite_residential_charge <=> >>> (min_price_var..max_price_var) >>> single_ensuite > 0 >>> elsif room_type_var == "twin" >>> twin_residential_charge <=> >>> (min_price_var..max_price_var) >>> twin > 0 >>> elsif room_type_var == "twin with ensuite" >>> twin_ensuite_residential_charge <=> >>> (min_price_var..max_price_var) >>> twin_ensuite > 0 >>> elsif room_type_var == "multi" >>> multi_residential_charge <=> >>> (min_price_var..max_price_var) >>> multi > 0 >>> elsif room_type_var == "multi with ensuite" >>> multi_ensuite_residential_charge <=> >>> (min_price_var..max_price_var) >>> multi_ensuite > 0 >>> end >>> end >>> end >>> >>> Thanks in advance. >>> Harvey >>> >>> >>> This e-mail has been scanned for all viruses by MessageLabs. >>> _______________________________________________ >>> Rails mailing list >>> Rails@lists.rubyonrails.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >> >> -Ezra Zygmuntowicz >> Yakima Herald-Republic >> WebMaster >> http://yakimaherald.com >> 509-577-7732 >> ezra@yakima-herald.com >> >> >> >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > > -- > Giles Goat Boy > > http://gilesmakesmusic.blogspot.com > http://gileswritescode.blogspot.com > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > This e-mail has been scanned for all viruses by MessageLabs.This e-mail has been scanned for all viruses by MessageLabs.