37 lines
936 B
Ruby
37 lines
936 B
Ruby
class Product
|
|
include SimplyStored::Couch
|
|
|
|
property :name
|
|
property :code
|
|
property :price, type: Float
|
|
|
|
#belongs_to :product_category
|
|
has_and_belongs_to_many :product_categories, storing_keys: false
|
|
belongs_to :supplier # direct! category is an aid
|
|
has_many :product_orders
|
|
|
|
attr_protected :supplier_id
|
|
|
|
validates :name, presence: true
|
|
validates :supplier_id, presence: true
|
|
validates :price, presence: true, numericality: true
|
|
view :by_supplier_id_and_id, key: [:supplier_id, :_id]
|
|
|
|
after_save :persist_product_category_ids
|
|
|
|
def product_category_ids=(ids)
|
|
@product_category_ids = ids.select(&:present?)
|
|
end
|
|
|
|
private
|
|
def persist_product_category_ids
|
|
return unless @product_category_ids.present?
|
|
database.load(@product_category_ids).each do |product_category|
|
|
product_category.product_ids ||= []
|
|
product_category.product_ids |= [id]
|
|
product_category.save
|
|
end
|
|
end
|
|
|
|
end
|