2012-03-16 2 views
1

안녕하세요. 장바구니에 항목을 추가하는 데 문제가 있습니다. 레일 민첩한 책에서. 속성이 다른 제품 (아파트, 자동차)에 추가하려면다른 제품 유형의 장바구니에 상품 추가

class Products 
    has_many :line_items 
    attributes :name, :price, :content 
end 

class LineItem 
    belongs_to :products 
    belongs_to :carts 
end 

class Cart 
    has_many :line_items 
end 

class Car 
    attributes :name, :car_type, :color 
end 

class Apartment 
    attributes :name, :size, :location 
end 

class Order 
    attr :buyer_details, :pay_type 
end 

고객은 장바구니에 제품을 추가하고 e.x. 임대료 2 침실, 임대료를 내고 리무진을 지불하고 싶습니다. 장바구니에 추가하는 방법. lineitems에 apartment_id 및 car_id를 입력하면 오염됩니까? 올바른 접근 방식과 올바른 습관이 필요합니다. 모두에게 감사드립니다.

답변

1

다형성 연관을 LineItems에 모두 보관하려면 연관 관계를 찾습니다. 그런 다음 LineItems를 제품, 아파트 및 자동차 용 폴리로 만듭니다. 하지만 디자인이 정말 나쁘다고 생각합니다. 구매와 임대는 매우 다릅니다. 임대시에는 이중 예약을해서는 안되는 기간, 단일 주소 또는 등록이 있습니다. 돌아가서 ERD에서 작업하십시오. 더 나은 디자인의

하나의 옵션 :

NB 내가 변경 한 LineItem을은 명확성을 위해 CartItem 될 수 있습니다.

class Products 
    has_many :cart_items 
    has_many :order_items 
    attributes :name, :price, :content 
end 


class Cart 
    has_many :line_items 
end 
class CartItem 
    belongs_to :products 
    belongs_to :carts 
end 
class CartRental 
    # :cart_rentable_type, :cart_rentable_id would be the fields for the polymorphic part 
    belongs_to :cart_rentable, :polymorphic => true 
    belongs_to :carts 
    attributes :from, :till 
end 

class Order 
    attr :buyer_details, :pay_type 
end 
class OrderItem 
    belongs_to :products 
    belongs_to :order 
end 
class Rental 
    belongs_to :rentable, :polymorphic => true 
    belongs_to :order 
    # :rentable_type, :rentable_id would be the fields for the polymorphic part 
    attributes :from, :till, :status 
end 


class Car 
    attributes :name, :car_type, :color 
    has_many :cart_rentals, :as => :cart_rentable 
    has_many :rentals, :as => :rentable 
end 
class Apartment 
    attributes :name, :size, :location 
    has_many :cart_rentals, :as => :cart_rentable 
    has_many :rentals, :as => :rentable 
end 
+0

그건 내 질문입니다. 그것이 나쁜 디자인 인 경우, 그것을하는 올바른 방법은 무엇입니까? –

+0

위의 답변이 당신을 위해 효과가있는 예제로 업데이트되었습니다. – TomDunning

관련 문제