2012-03-16 30 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ベッドルームの賃貸料、リムジンの賃料と支払いを望みます。カートに追加する方法。私がapartment_idとcar_idをlineitemsに入れたら、それは汚染されますか?正しいアプローチ、正しい練習が必要です。全てに感謝。

答えて

1

LineItems内にすべてを保持したい場合は、多態性の関連付けをルックアップします。その後、LineItemsを商品、アパート、車のポリゴンにします。でもデザインは本当に悪いと思う。購入とレンタルは非常に異なっています。賃貸借では、期間、単一の住所または登録が必要です。戻ってERDで作業してください。

よりよい設計の1つの選択肢:

NB iは、明確にするためCartItemされるのLineItemを変更しました。

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

関連する問題