product_item.rb 1.3 KB

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