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”}
e[4;36;1mHome Count (0.002739)e[0m e[0;1mSELECT COUNT() FROM homes
WHERE (postcode LIKE ‘%%’) e[0m
e[4;35;1mHome Count (0.002695)e[0m e[0mSELECT COUNT() FROM homes
WHERE
(postcode LIKE ‘%%’) e[0m
e[4;36;1mHome Load (0.003985)e[0m e[0;1mSELECT * FROM homes WHERE
(postcode LIKE ‘%%’) LIMIT 0, 25e[0m
Rendering layoutfalseactionlist within layouts/application Rendering
home/list Rendered shared/_list_left_side (0.00054)
e[4;35;1mHome Columns (0.001351)e[0m e[0mSHOW FIELDS FROM homese[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 Z.” [email protected] 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:
Ruby on Rails Blog / What is Ruby on Rails for?
Cheers-
-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
[email protected]
509-577-7732
This e-mail has been scanned for all viruses by MessageLabs.
Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails
Ezra Z. 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:
Ruby on Rails Blog / What is Ruby on Rails for?
Cheers-
-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
[email protected]
509-577-7732