Forum: Ruby Wrong realization of method?

F559a5faed904d7558efd65e281a79f3?d=identicon&s=25 Eugene Greenwood (redkino)
on 2013-10-25 08:06
Can anybody help me?

I'v got the function (method) that was written before me. I have to use
this method. I think that I can't understand several primitive things,
but I (may be) don't know about these things.

Questions:
When this function can (may) returns nil?
I'v got incidents when I know that this function must returns me a
record from Oracle. But I receive return value = nil. Why? Reasons?

The text of ruby method is below...

# find cm_batch_id by given invoice number, shipments_status_id,
# batches_status_id
#Example:
#CmShipment.find_cm_batch_id_by_invoice_and_status_id(@lneIInvoice.text,
#CM_SHIPMEMTS_STATUSID_ERR_SHIPMENT,CM_SHIPMEMTS_STATUSID_INITIAL)
def
self.find_cm_batch_id_by_invoice_and_status_id(invoice,shipments_status_id,batches_status_id)
begin
sql_statement = <<-END_OF_SQL
SELECT cm_batch_id
FROM CM_SHIPMENTS sp
WHERE i_invoice=#{invoice}
AND status_id=#{shipments_status_id}
AND EXISTS (SELECT 1 FROM CM_BATCHES bh
WHERE sp.cm_batch_id = bh.id
AND bh.status_id=#{batches_status_id})
END_OF_SQL
return CmShipment.find_by_sql(sql_statement)
rescue
return nil
end
end

P.S. Execuse me my bad English :) I'm from Russia :)
3853dd5371ac1e094fc45d6c2aa0e459?d=identicon&s=25 Carlo E. Prelz (Guest)
on 2013-10-25 08:56
(Received via mailing list)
Subject: Wrong realization of method?
  Date: ven 25 ott 13 08:06:35 +0200

Quoting Eugene Greenwood (lists@ruby-forum.com):

> I'v got the function (method) that was written before me. I have to use
> this method. I think that I can't understand several primitive things,
> but I (may be) don't know about these things.
>
> Questions:
> When this function can (may) returns nil?
> I'v got incidents when I know that this function must returns me a
> record from Oracle. But I receive return value = nil. Why? Reasons?

Well, can't you see where the 'return nil' statement is? The method
returns nil when rescue is executed. This means that the find_by_sql
method has raised an exception.

If you want to know what exactly is going wrong, add a printout before
returning nil.

Carlo
F559a5faed904d7558efd65e281a79f3?d=identicon&s=25 Eugene Greenwood (redkino)
on 2013-10-25 10:36
Carlo E. Prelz wrote in post #1125576:
> Subject: Wrong realization of method?
>   Date: ven 25 ott 13 08:06:35 +0200
>
> Quoting Eugene Greenwood (lists@ruby-forum.com):
>
>> I'v got the function (method) that was written before me. I have to use
>> this method. I think that I can't understand several primitive things,
>> but I (may be) don't know about these things.
>>
>> Questions:
>> When this function can (may) returns nil?
>> I'v got incidents when I know that this function must returns me a
>> record from Oracle. But I receive return value = nil. Why? Reasons?
>
> Well, can't you see where the 'return nil' statement is? The method
> returns nil when rescue is executed. This means that the find_by_sql
> method has raised an exception.
>
> If you want to know what exactly is going wrong, add a printout before
> returning nil.
>

Carlo, thanks very much for your attention and your reply!

Usually I make such things using "puts" in place of "printout"
for debug purposes. "Puts", after rescue, say me that parameter
sql_statement is what I need... And I make the same sql-request from
console... and I'v got a record from console.

May be it's bug from ActiveRecord?
820e01609a9f8f0fc0d5167680625b93?d=identicon&s=25 Harisankar P S (coderhs)
on 2013-10-25 12:08
(Received via mailing list)
On Fri, Oct 25, 2013 at 2:06 PM, Eugene Greenwood
<lists@ruby-forum.com>wrote:

>
>
> Carlo, thanks very much for your attention and your reply!
>
> Usually I make such things using "puts" in place of "printout"
> for debug purposes. "Puts", after rescue, say me that parameter
> sql_statement is what I need... And I make the same sql-request from
> console... and I'v got a record from console.
>
>
So u ran the same SQL statement from the console and got the required
out
put? Can you do one thing,
 can you remove the begin, and rescue, so that we can know the actually
error that is returned. From that
error we will get a clearer picture if the error is from active record
or
not.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.