Guest User

Untitled

a guest
Feb 20th, 2018
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. Any ideas on why this is? The quantity field in mysql is INT, but I get back a string? This is preventing me from using the increment method.
  2.  
  3. >> @cart = Cart.new
  4. => #<Cart:0x2730f40 @new_record=true, @attributes={}>
  5. >> @cart.items << Item.find(:first)
  6. => [#<Item:0x272d6ec @attributes={"vendor_id"=>"1", "updated_at"=>"2006-09-26 12:00:38", "catalog_id"=>"8050", "price"=>"185", "created_by"=>"Unit Tests", "unit"=>"ea", "updated_by"=>nil, "id"=>"1", "description"=>"CAP analog for SP6 MegaScript kit", "created_at"=>nil}>]
  7. >> @cart.save
  8. => true
  9. >> @cart.items.find(1).quantity
  10. => "1"
  11. >> @cart.items.find(1).increment!(:quantity)
  12. TypeError: can't convert Fixnum into String
  13. from /usr/local//lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1445:in `+'
  14. from /usr/local//lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1445:in `increment'
  15. from /usr/local//lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:1451:in `increment!'
  16. from (irb):8
  17.  
  18. Using
  19. -----
  20. Rails 1.1.6
  21. Mac OS 10.4.7
  22. mysql Ver 14.7 Distrib 4.1.20, for apple-darwin8.5.1 (i686) using readline 4.3
  23.  
  24.  
  25. mysql> desc carts_items;
  26. +----------+---------+------+-----+---------+-------+
  27. | Field | Type | Null | Key | Default | Extra |
  28. +----------+---------+------+-----+---------+-------+
  29. | cart_id | int(11) | | | 0 | |
  30. | item_id | int(11) | | | 0 | |
  31. | quantity | int(11) | | | 1 | |
  32. +----------+---------+------+-----+---------+-------+
  33. 3 rows in set (0.10 sec)
  34.  
  35.  
  36. mysql> desc carts;
  37. +-------+---------+------+-----+---------+----------------+
  38. | Field | Type | Null | Key | Default | Extra |
  39. +-------+---------+------+-----+---------+----------------+
  40. | id | int(11) | | PRI | NULL | auto_increment |
  41. +-------+---------+------+-----+---------+----------------+
  42. 1 row in set (0.00 sec)
  43.  
  44. mysql> desc items;
  45. +-------------+--------------+------+-----+---------+----------------+
  46. | Field | Type | Null | Key | Default | Extra |
  47. +-------------+--------------+------+-----+---------+----------------+
  48. | id | int(11) | | PRI | NULL | auto_increment |
  49. | vendor_id | int(11) | YES | | NULL | |
  50. | catalog_id | varchar(255) | YES | | NULL | |
  51. | description | varchar(255) | YES | | NULL | |
  52. | unit | varchar(255) | YES | | NULL | |
  53. | price | float | YES | | NULL | |
  54. | created_at | datetime | YES | | NULL | |
  55. | updated_at | datetime | YES | | NULL | |
  56. | created_by | varchar(255) | YES | | NULL | |
  57. | updated_by | varchar(255) | YES | | NULL | |
  58. +-------------+--------------+------+-----+---------+----------------+
  59. 10 rows in set (0.00 sec)
  60.  
  61. cart.rb
  62. class Cart < ActiveRecord::Base
  63. has_and_belongs_to_many :items
  64. end
  65.  
  66. item.rb
  67. class Item < ActiveRecord::Base
  68. has_and_belongs_to_many :carts
  69. end
Add Comment
Please, Sign In to add comment