Data retrieval with :conditions


#1

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


#2

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


#3

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!