product_item.rb 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. 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 :title
  35. field :item_id
  36. field :item_title
  37. field :nums
  38. end
  39. show do
  40. field :id
  41. field :product_id
  42. field :item_id
  43. field :nums
  44. end
  45. edit do
  46. field :product_id
  47. field :item_id
  48. field :nums
  49. end
  50. end
  51. end