newbie stuck with multiple conditions

M

Martin Robertson

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
 
J

Jacob Basham

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
 
M

Martin Robertson

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
 
J

Jacob Basham

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top