Guest User

Untitled

a guest
Jun 20th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. ## Order class
  2. class Order < ActiveRecord::Base
  3. has_many :productorders do
  4. def get_amount_by_product_id(product_id)
  5. po = Productorders.find_by_order_id(self).find_by_product_id(product_id)
  6. if po.nil?
  7. return 0
  8. else
  9. return po.amount
  10. end
  11. end
  12. end
  13. has_many :products, :through=>:productorders
  14. belongs_to :customer
  15. end
  16.  
  17. ## Productorder is the join table of order and product
  18.  
  19. class Productorder < ActiveRecord::Base
  20. belongs_to :product
  21. belongs_to :order
  22. end
  23.  
  24. ## Migration of productorder
  25. class CreateProductorders < ActiveRecord::Migration
  26. def self.up
  27. create_table :productorders, :id=>false do |t|
  28. t.integer :amount
  29. t.integer :product_id
  30. t.integer :order_id
  31.  
  32. t.timestamps
  33. end
  34. end
  35.  
  36. def self.down
  37. drop_table :productorders
  38. end
  39. end
  40.  
  41.  
  42. ## Product class
  43. class Product < ActiveRecord::Base
  44. has_many :productorders
  45. has_many :orders, :through => :productorders
  46. end
Add Comment
Please, Sign In to add comment