Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
newbie stuck with multiple conditions
Martin Robertson
11/12/2007 12:48:00 AM
Hi
I am new to rails and programming.
I am trying to get a conditional search from form results for a property
search.
I want to be able to contruct a search that will search (:all) if ID for
all is returned or by individual ID
The code below does not work for the all condition
def results
suburb_id=params[:suburb][:id]
bedroom_id=params[:bedroom][:id]
garage_id=params[:garage][:id]
@listings=ListingDetail.find(
:all,
:conditions =>["suburb_id =:suburb_id and bedroom_id=:bedroom_id and
garage_id=:garage_id",
{
:suburb_id =>suburb_id,
:bedroom_id=>bedroom_id,
:garage_id=>garage_id
}
]
)
end
I have also tried use case and if as below
def results
bedroom_id=params[:bedroom][:id]
case bedroom_id
when 6
@listings=ListingDetail.find_all_by_bedroom_id(:all)
else
@listings=ListingDetail.find_all_by_bedroom_id(bedroom_id)
end
garage_id=params[:garage][:id]
case garage_id
when 4
@listings=ListingDetail.find_all_by_garage_id(:all)
else
@listings=ListingDetail.find_all_by_garage_id(garage_id)
end
end
Any help would be appreciated
Martin
--
Posted via
http://www.ruby-...
.
3 Answers
Jacob Basham
11/12/2007 3:11:00 AM
0
1. You should be posting to the rails list, not the ruby list.
2. Conditions takes an array OR a dictionary. I believe what you are
trying to do is the following:
@listings = ListingDetail.find(:all, :conditions = ["suburb_id = ? AND
bedroom_id = ? AND garage_id = ?", params[:suburb_id], params
[:bedroom_id], params[:garage_id]])
3. There is a logic error in your second case, calling
type_find_by_other_id, you need to pass an actual id. :all means find
all. If you want to find all, its just find(:all).
And for your object model, I'm thinking a listing should have a int
number rooms and either int number car garage, or bool if has garage.
Sorry if this isn't more informative, but I'm on the bus and its bumpy.
Jake
Sent from my iPhone
On Nov 11, 2007, at 6:47 PM, Martin Robertson
<info@martinrobertson.com> wrote:
> Hi
>
> I am new to rails and programming.
>
> I am trying to get a conditional search from form results for a
> property
> search.
>
> I want to be able to contruct a search that will search (:all) if ID
> for
> all is returned or by individual ID
>
> The code below does not work for the all condition
>
> def results
>
> suburb_id=params[:suburb][:id]
> bedroom_id=params[:bedroom][:id]
> garage_id=params[:garage][:id]
> @listings=ListingDetail.find(
> :all,
> :conditions =>["suburb_id =:suburb_id and bedroom_id=:bedroom_id and
> garage_id=:garage_id",
> {
> :suburb_id =>suburb_id,
> :bedroom_id=>bedroom_id,
> :garage_id=>garage_id
> }
> ]
> )
> end
>
> I have also tried use case and if as below
>
> def results
>
> bedroom_id=params[:bedroom][:id]
> case bedroom_id
> when 6
> @listings=ListingDetail.find_all_by_bedroom_id(:all)
> else
> @listings=ListingDetail.find_all_by_bedroom_id(bedroom_id)
> end
> garage_id=params[:garage][:id]
> case garage_id
> when 4
> @listings=ListingDetail.find_all_by_garage_id(:all)
> else
> @listings=ListingDetail.find_all_by_garage_id(garage_id)
> end
>
> end
>
> Any help would be appreciated
>
> Martin
> --
> Posted via
http://www.ruby-...
.
>
Martin Robertson
11/12/2007 4:38:00 AM
0
Hi Jacob
I have tried what you have suggested
@listings = ListingDetail.find(:all, :conditions => ["suburb_id = ? AND
bedroom_id = ? AND garage_id = ?", params[:suburb_id], params
[:bedroom_id], params[:garage_id]])
but it throws up errors as
app/controllers/admin/listing_details_controller.rb:55: syntax error,
unexpected tLBRACK, expecting ']'
[:bedroom_id], params[:garage_id]])
^
app/controllers/admin/listing_details_controller.rb:55: syntax error,
unexpected ',', expecting kEND
[:bedroom_id], params[:garage_id]])
^
app/controllers/admin/listing_details_controller.rb:55: syntax error,
unexpected ']', expecting kEND
[:bedroom_id], params[:garage_id]])
^
app/controllers/admin/listing_details_controller.rb:85: syntax error,
unexpected $end, expecting kEND
I have also tried a variation of this
def results
suburb_id=params[:suburb][:id]
bedroom_id=params[:bedroom][:id]
garage_id=params[:garage][:id]
@listings=ListingDetail.find(
:all,
:conditions =>["suburb_id =:suburb_id and bedroom_id=:bedroom_id and
garage_id=:garage_id",
{
:suburb_id =>suburb_id,
:bedroom_id=>bedroom_id,
:garage_id=>garage_id
}
]
)
end
this produces error message
Showing app/views/admin/listing_details/results.rhtml where line #2
raised:
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
Extracted source (around line #2):
1: <ul>
2: <% for listings in @listings %>
3: <li><%=listings.descriptionthumb %></li>
4: <% end %>
5: </ul>
RAILS_ROOT: ./script/../config/..
(--deleted all this stuff--)
Request
Parameters: {"garage"=>{"id"=>"1"}, "commit"=>"Search",
"suburb"=>{"id"=>"2"}, "bedroom"=>{"id"=>"1"}}
I have also posted this as new post in correct rails forum as advised
Martin
--
Posted via
http://www.ruby-...
.
Jacob Basham
11/12/2007 9:28:00 PM
0
Martin,
Your "variation" is doing the same as what you had before in your
first email. Also creating variables with your parameters is not
necessary, pointless really. And yes, what I put was a bit off, as I
said I was on the bus. I believe you can go
@listings = ListingDetail.find(:all, :conditions => {:garage_id =>
params[:garage][:id], :room_id => params[:room][:id], :suburb_id =>
params[:suburb][:id] }
What this is doing is creating a Hash of their key/value pairs, and
passing that Hash as the conditions for the find query. If you look at
the console window you are running the rails server in, you should see
something like
SELECT * FROM ListingDetails WHERE garage_id = x AND room_id = y AND
suburb_id = z
Now, you need to be careful because you will get into some tricky
business if either of your three id's are not passed in your search.
That should give you enough to get running ,but like I said before
this is really something that should be discussed on the rails mailing
list.
Good Luck,
Jake
On Nov 11, 2007, at 10:37 PM, Martin Robertson wrote:
> Hi Jacob
>
> I have tried what you have suggested
>
> @listings = ListingDetail.find(:all, :conditions => ["suburb_id = ?
> AND
> bedroom_id = ? AND garage_id = ?", params[:suburb_id], params
> [:bedroom_id], params[:garage_id]])
>
> but it throws up errors as
>
> app/controllers/admin/listing_details_controller.rb:55: syntax error,
> unexpected tLBRACK, expecting ']'
> [:bedroom_id], params[:garage_id]])
> ^
> app/controllers/admin/listing_details_controller.rb:55: syntax error,
> unexpected ',', expecting kEND
> [:bedroom_id], params[:garage_id]])
> ^
> app/controllers/admin/listing_details_controller.rb:55: syntax error,
> unexpected ']', expecting kEND
> [:bedroom_id], params[:garage_id]])
> ^
> app/controllers/admin/listing_details_controller.rb:85: syntax error,
> unexpected $end, expecting kEND
>
> I have also tried a variation of this
>
> def results
>
> suburb_id=params[:suburb][:id]
> bedroom_id=params[:bedroom][:id]
> garage_id=params[:garage][:id]
> @listings=ListingDetail.find(
> :all,
> :conditions =>["suburb_id =:suburb_id and bedroom_id=:bedroom_id and
> garage_id=:garage_id",
> {
> :suburb_id =>suburb_id,
> :bedroom_id=>bedroom_id,
> :garage_id=>garage_id
> }
> ]
> )
> end
>
> this produces error message
>
> Showing app/views/admin/listing_details/results.rhtml where line #2
> raised:
>
> You have a nil object when you didn't expect it!
> You might have expected an instance of Array.
> The error occurred while evaluating nil.each
>
> Extracted source (around line #2):
>
> 1: <ul>
> 2: <% for listings in @listings %>
> 3: <li><%=listings.descriptionthumb %></li>
> 4: <% end %>
> 5: </ul>
>
>
> RAILS_ROOT: ./script/../config/..
>
> (--deleted all this stuff--)
>
> Request
> Parameters: {"garage"=>{"id"=>"1"}, "commit"=>"Search",
> "suburb"=>{"id"=>"2"}, "bedroom"=>{"id"=>"1"}}
>
> I have also posted this as new post in correct rails forum as advised
>
> Martin
> --
> Posted via
http://www.ruby-...
.
>
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
newbie stuck with multiple conditions
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password