2014-12-04 19:50:02 +01:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: orders
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# user_id :integer
|
|
|
|
# cost :integer
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
#
|
|
|
|
|
2014-11-09 22:53:39 +01:00
|
|
|
class Order < ActiveRecord::Base
|
2014-11-25 21:34:29 +01:00
|
|
|
belongs_to :user
|
2014-12-04 19:50:02 +01:00
|
|
|
|
2014-11-25 14:27:27 +01:00
|
|
|
has_many :order_products
|
2014-11-29 11:06:22 +01:00
|
|
|
has_many :products, -> { includes :order_product }, { through: :order_products} do
|
|
|
|
def << (product)
|
|
|
|
if proxy_association.owner.products.include?(product)
|
|
|
|
proxy_association.owner.order_products.find_by(product: product).increment!(:count, 1)
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-11-25 14:27:27 +01:00
|
|
|
|
2014-12-04 19:50:02 +01:00
|
|
|
validates :user, presence: true
|
|
|
|
|
2014-12-04 17:02:08 +01:00
|
|
|
accepts_nested_attributes_for :order_products
|
|
|
|
|
2014-11-10 22:57:37 +01:00
|
|
|
default_scope -> { order('created_at DESC') }
|
2014-11-25 02:01:57 +01:00
|
|
|
|
2014-11-09 22:53:39 +01:00
|
|
|
end
|