Can't convert true into integer

I have a method:

 def reduceProuctInventoryBy(product_id, reduction)
  t=Product.find(:first, :conditions =>[“id = ?”, product_id])
  t.qty=(t.qty-reduction)
  t.save
 end

and the model:

class Product < ActiveRecord::Base
 validates_presence_of :qty, :specialDescription
 validates_numericality_of :qty
end

but when i run my method, I get this:

“can’t convert true into Integer”

and rails does not give me a line number in the trace the error back to,
any ideas?

ps
I am positive that Product.find(:first, :conditions =>[“id = ?”,
product_id]) actually returns something from my database, since i use
the exact same statement in another method that just returns a product.

On Thursday, July 13, 2006, at 5:53 AM, Jon wrote:

class Product < ActiveRecord::Base

[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

I don’t think we can decipher this one without seeing the table schema.

FYI…

def reduceProuctInventoryBy(product_id, reduction)
t=Product.find(product_id)
t.qty -= reduction
t.save
end

_Kevin

On 7/15/06, Jon [email protected] wrote:

I’m still at a loss on this one.

What are you passing in for “reduction”?

Kevin O. wrote:

I don’t think we can decipher this one without seeing the table schema.

Thank you for responding. I made the change you suggested, but still got
the same error. Here is my schema:

class CreateProducts < ActiveRecord::Migration
 def self.up
  create_table :products do |t|
   t.column :qty, :integer, :default => 0
   t.column :specialDescription, :string
   end
 end

 def self.down
  drop_table :products
 end
end

I’m still at a loss on this one.

-Jon

Jon wrote:

I have a method:

 def reduceProuctInventoryBy(product_id, reduction)
  t=Product.find(:first, :conditions =>[“id = ?”, product_id])
  t.qty=(t.qty-reduction)
  t.save
 end

Web service API line:
api_method :reduceProuctInventoryBy,
:expects=>[{:product_id=>:integer},{:reduction=>:integer}],Â
:returns=>[:integer]

Well I managed to fix it. I spend a day trying to track down a bug…
then I sit down days later, and I find the issue in 5 mins.

Anyway, If you notice, in my service return line, I guarantee that I
return an int. In my origanal code, I don’t have a return statement. I
modified my method and everything works just peachy:

 def reduceProuctInventoryBy(product_id, reduction)
  t=Product.find(:first, :conditions =>[“id = ?”, product_id])
  t.qty-=reduction
  t.save
  return t.qty
 endÂ

James L. wrote:

On 7/15/06, Jon [email protected] wrote:

I’m still at a loss on this one.

What are you passing in for “reduction”?

 Since this is a web service method, I have it typed to an integer. Here
is the web service API line:

 api_method :reduceProuctInventoryBy,
:expects=>[{:product_id=>:integer},{:reduction=>:integer}],
:returns=>[:integer]

If I try to pass in a letter in the invoke pages, rails barks at me and
tells me it’s not a valid integer.

On Sunday, July 16, 2006, at 1:00 AM, Jon wrote:

api_method :reduceProuctInventoryBy,
def reduceProuctInventoryBy(product_id, reduction)
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Ah yes. save returns a true or false, and you were expecting an int.
Makes sense.

_Kevin
www.sciwerks.com

Justin F. wrote:

Hi, Jon

Glad you solved it. Shouldn’t that be “reduceProductInventoryBy” ?
(Product, not Prouct)

Not surprising nobody could see what was wrong - there’s no way we could
tell from the method name that you intended to return the updated quantity.

You don’t need to put “return” in there - just have t.qty as the last
expression in the method. Previously, the method was returning true,
which is the result of a successful t.save.

Also, you can simplify your find:

t = Product.find(product_id)

This will throw an exception if there is no product with that id, but
your current code would also throw an exception if your find(:first…)
returned nil.

regards

Justin

Jon wrote:
[…]

def reduceProuctInventoryBy(product_id, reduction)
t=Product.find(:first, :conditions =>[“id = ?”, product_id])
t.qty-=reduction
t.save
return t.qty
end

Hi, Jon

Glad you solved it. Shouldn’t that be “reduceProductInventoryBy” ?
(Product, not Prouct)

Not surprising nobody could see what was wrong - there’s no way we could
tell from the method name that you intended to return the updated
quantity.

You don’t need to put “return” in there - just have t.qty as the last
expression in the method. Previously, the method was returning true,
which is the result of a successful t.save.

regards

Justin