Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Order class
- class Order < ActiveRecord::Base
- has_many :productorders do
- def get_amount_by_product_id(product_id)
- po = Productorders.find_by_order_id(self).find_by_product_id(product_id)
- if po.nil?
- return 0
- else
- return po.amount
- end
- end
- end
- has_many :products, :through=>:productorders
- belongs_to :customer
- end
- ## Productorder is the join table of order and product
- class Productorder < ActiveRecord::Base
- belongs_to :product
- belongs_to :order
- end
- ## Migration of productorder
- class CreateProductorders < ActiveRecord::Migration
- def self.up
- create_table :productorders, :id=>false do |t|
- t.integer :amount
- t.integer :product_id
- t.integer :order_id
- t.timestamps
- end
- end
- def self.down
- drop_table :productorders
- end
- end
- ## Product class
- class Product < ActiveRecord::Base
- has_many :productorders
- has_many :orders, :through => :productorders
- end
Add Comment
Please, Sign In to add comment