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.
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
 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Â
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.
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.