product_item.rb 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # encoding:utf-8
  2. class ProductItem < ActiveRecord::Base
  3. has_paper_trail
  4. self.table_name = "product_items"
  5. after_commit :clear_product_cache
  6. validates :product_id,:item_id,:nums, presence: true
  7. validate :product_validation
  8. def product_validation
  9. if self.product_id > 0
  10. prd = Product.where("id = ?", self.product_id).first
  11. if prd.blank?
  12. self.errors.add(:product_id,"商品不存在,请重新填写商品ID")
  13. else
  14. if !prd.package
  15. self.errors.add(:product_id,"商品非套装商品,请重新录入!")
  16. end
  17. self.title=prd.name
  18. end
  19. end
  20. if self.item_id > 0
  21. prd = Product.where("id = ?", self.item_id).first
  22. if prd.blank?
  23. self.errors.add(:item_id,"商品不存在,请填写正确的明细ID")
  24. else
  25. self.item_title=prd.name
  26. end
  27. end
  28. end
  29. def clear_product_cache
  30. Product.notify_product_cache(self.product_id)
  31. end
  32. rails_admin do
  33. navigation_label '套装商品明细'
  34. weight -300
  35. parent Product
  36. list do
  37. filters [:product_id,:item_id]
  38. # include_all_fields
  39. field :id
  40. field :product_id
  41. field :title
  42. field :item_id
  43. field :item_title
  44. field :nums
  45. end
  46. show do
  47. field :id
  48. field :product_id
  49. field :item_id
  50. field :nums
  51. end
  52. edit do
  53. field :product_id
  54. field :item_id
  55. field :nums
  56. end
  57. end
  58. end