# Data retrieval with :conditions

**URL:** <https://www.ruby-forum.com/t/data-retrieval-with-conditions/103894>\
**Category:** Rails\
**Created:** [May 11, 2007, 12:30pm UTC](https://www.ruby-forum.com/t/data-retrieval-with-conditions/103894 "2007-05-11T12:30:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elmy-liddiard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c67d28/32.png) [@elmy-liddiard](https://www.ruby-forum.com/u/elmy-liddiard)\
**Post date:** [May 11, 2007, 12:30pm UTC](https://www.ruby-forum.com/t/data-retrieval-with-conditions/103894/1 "2007-05-11T12:30:33Z")

</div>

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:-

Status 

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

---

<div class="post-metadata">

**Author:** ![elmy-liddiard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c67d28/32.png) [@elmy-liddiard](https://www.ruby-forum.com/u/elmy-liddiard)\
**Post date:** [May 11, 2007, 3:46pm UTC](https://www.ruby-forum.com/t/data-retrieval-with-conditions/103894/2 "2007-05-11T15:46:28Z")

</div>

Hi Martyn,

Martyn Elmy-liddiard wrote:

> Obviously my Model code is wrong

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

> Status

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

---

<div class="post-metadata">

**Author:** ![elmy-liddiard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c67d28/32.png) [@elmy-liddiard](https://www.ruby-forum.com/u/elmy-liddiard)\
**Post date:** [May 11, 2007, 5:26pm UTC](https://www.ruby-forum.com/t/data-retrieval-with-conditions/103894/3 "2007-05-11T17:26:57Z")

</div>

Bill W. wrote:

> Hi Martyn,
> 
> Martyn Elmy-liddiard wrote:
> 
> > Obviously my Model code is wrong
> 
> No offense intended, but there’re problems with all your code.
> 
> > Status
> 
> 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!
