product_item.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 :order_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. self.title=prd.name
  14. end
  15. end
  16. if self.item_id > 0
  17. prd = Product.where("id = ?", self.item_id).first
  18. if prd.blank?
  19. self.errors.add(:item_id,"商品不存在,请填写正确的明细ID")
  20. else
  21. self.item_title=prd.name
  22. end
  23. end
  24. end
  25. rails_admin do
  26. navigation_label '套装商品明细'
  27. weight -300
  28. parent Product
  29. list do
  30. filters [:product_id,:item_id]
  31. # include_all_fields
  32. field :id
  33. field :product_id
  34. field :item_id
  35. field :nums
  36. end
  37. show do
  38. field :id
  39. field :product_id
  40. field :item_id
  41. field :nums
  42. end
  43. edit do
  44. field :id
  45. field :product_id
  46. field :item_id
  47. field :nums
  48. end
  49. end
  50. end