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. -- Posted via http://www.ruby-forum.com/.
On Thursday, July 13, 2006, at 5:53 AM, 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 > >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. > >-- >Posted via http://www.ruby-forum.com/. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railsI 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 -- Posted with http://DevLists.com. Sign up and save your mailbox.
Kevin Olbrich 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 -- Posted via http://www.ruby-forum.com/.
On 7/15/06, Jon <root@localhost.net> wrote:> I''m still at a loss on this one.What are you passing in for "reduction"?
James Ludlow wrote:> On 7/15/06, Jon <root@localhost.net> 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. -- Posted via http://www.ruby-forum.com/.
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? -- Posted via http://www.ruby-forum.com/.
On Sunday, July 16, 2006, at 1:00 AM, Jon wrote:>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 > >-- >Posted via http://www.ruby-forum.com/. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/railsAh yes. save returns a true or false, and you were expecting an int. Makes sense. _Kevin www.sciwerks.com -- Posted with http://DevLists.com. Sign up and save your mailbox.
Jon wrote: [...]> > def reduceProuctInventoryBy(product_id, reduction) > t=Product.find(:first, :conditions =>["id = ?", product_id]) > t.qty-=reduction > t.save > return t.qty > endHi, 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
Justin Forder wrote:> 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.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