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/at...
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
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)[0m [0;1mSELECT COUNT(*) FROM homes WHERE (postcode LIKE '%%') [0m [4;35;1mHome Count (0.002695)[0m [0mSELECT COUNT(*) FROM homes WHERE (postcode LIKE '%%') [0m [4;36;1mHome Load (0.003985)[0m [0;1mSELECT * FROM homes WHERE (postcode LIKE '%%') LIMIT 0, 25[0m Rendering layoutfalseactionlist within layouts/application Rendering home/list Rendered shared/_list_left_side (0.00054) [4;35;1mHome Columns (0.001351)[0m [0mSHOW 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 -- Posted via http://www.ruby-forum.com/.
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)[0m [0;1mSELECT COUNT(*) FROM
> homes
> WHERE (postcode LIKE '%%') [0m
> [4;35;1mHome Count (0.002695)[0m [0mSELECT COUNT(*) FROM homes
> WHERE
> (postcode LIKE '%%') [0m
> [4;36;1mHome Load (0.003985)[0m [0;1mSELECT * FROM homes WHERE
> (postcode LIKE '%%') LIMIT 0, 25[0m
> Rendering layoutfalseactionlist within layouts/application Rendering
> home/list Rendered shared/_list_left_side (0.00054)
> [4;35;1mHome Columns (0.001351)[0m [0mSHOW 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