Forum: Ruby on Rails Data retrieval with :conditions

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
735d0124c56d45692b09ce5d758363bc?d=identicon&s=25 Martyn Elmy-liddiard (elmy-liddiard)
on 2007-05-11 12:30
Relatively new to Ruby but sticking with it, however I really need some
help on passing parameters.

I have a table of risks with a status field (values = Open Closed or
Parked.)

I have a view where user can enter the Status they wnat to retrieve all
matching records for.  View Code:-

<form action="/risk/liststatus2">
Status <input type="text" name="Status" size="20"><p>
<input type="submit">
</form>

In the Risk Controller I have the following Code:-
def liststatus2
 @found = Risk.liststatus2
end

and finally in the Model I have the following code:-
def self.liststatus2
   find(:all, :conditions => "Status = ?", @Status)
end

Obviously my Model code is wrong since if I use the following code:-
   find(:all, :conditions => "Status = 'Open'")
i.e. hard-coding the value 'Open' then it works fine.

So, in short, can someone please advise the syntax for passing the
Status parameter into the model to retrieve the records the user wants.

I have spent alot of time trying to sort this and learned alot of other
useful stuff in the process...so I'm not just running for help straight
away.

Having said that I really do need some assitance.

Any help gratefully recieved.

Thanks Martyn
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2007-05-11 15:46
(Received via mailing list)
Hi Martyn,

Martyn Elmy-liddiard wrote:

> Obviously my Model code is wrong

No offense intended, but there're problems with all your code.

> <form action="/risk/liststatus2">
> Status <input type="text" name="Status" size="20"><p>
> <input type="submit">
> </form>

You're hardcoding your form instead of using eRb.  Rails will have a
hard
time figuring out what you're asking it to do.  You'll need to decide
whether you want your params bound to a model or not: text_field vs.
text_field_tag.

 > In the Risk Controller I have the following Code:-
> def liststatus2
> @found = Risk.liststatus2
> end

Params get passed back to the controller in a hash.  In the case above,
the
value is coming back in a param you'd address like:

@found = params[:Status]

> and finally in the Model I have the following code:-
> def self.liststatus2
>   find(:all, :conditions => "Status = ?", @Status)
> end

The value is in @found.

hth,
Bill
735d0124c56d45692b09ce5d758363bc?d=identicon&s=25 Martyn Elmy-liddiard (elmy-liddiard)
on 2007-05-11 17:26
Bill Walton wrote:
> Hi Martyn,
>
> Martyn Elmy-liddiard wrote:
>
>> Obviously my Model code is wrong
>
> No offense intended, but there're problems with all your code.
>
>> <form action="/risk/liststatus2">
>> Status <input type="text" name="Status" size="20"><p>
>> <input type="submit">
>> </form>
>
> You're hardcoding your form instead of using eRb.  Rails will have a
> hard
> time figuring out what you're asking it to do.  You'll need to decide
> whether you want your params bound to a model or not: text_field vs.
> text_field_tag.
>
>  > In the Risk Controller I have the following Code:-
>> def liststatus2
>> @found = Risk.liststatus2
>> end
>
> Params get passed back to the controller in a hash.  In the case above,
> the
> value is coming back in a param you'd address like:
>
> @found = params[:Status]
>
>> and finally in the Model I have the following code:-
>> def self.liststatus2
>>   find(:all, :conditions => "Status = ?", @Status)
>> end
>
> The value is in @found.
>
> hth,
> Bill

Absolutely no offence taken!...I'm always happy to l;earn from others.
Unfortunately I dont quite understand your response. Would it be
possible for you to show me an example of how you would code this same
requirement. I can then decode that and understand how the best way to
do it is.

Much appreciated!
This topic is locked and can not be replied to.