协会混乱

时间:2011-01-10 08:31:01

标签: ruby-on-rails associations

情景:

示例:

卖方决定出售宝马。在销售特定型号的宝马(比如7系列760 Li)之前,需要由卖家创建CarSketch。这辆CarSketch属于宝马,在这里我们得到了关于宝马的规格。此CarSketch还快速总结了作为演示文稿表示的BMW(长度等)的物理信息。

因此,如果我想卖宝马。模型540和模型760。

# car_sketch_for_model_540 = CarSketch obj
# presentation_for_model_540 = Presentation obj

# car_sketch_for_model_760 = CarSketch obj
# presentation_for_model_760 = Presentation obj

卖家批准并开始销售宝马汽车,型号540和760.也就是说,买家只看到演示文稿列表。

由于汽车可以有很多买家,买家只会选择(购买)一个型号(演示)。

卖方将CarSketch模型用于内部物品。浏览网站时,买家可以看到可购买的型号,因为rails会通过CarSketch模型查看演示文稿。

对于对特定模型(演示文稿)感兴趣的每个买方,添加指向演示文稿的买方记录。

我在尝试弄清楚如何在Rails上设置这些关联时遇到问题。基本上我有4个型号。我们称之为汽车,汽车,演示和买家。

  1. 汽车可以有很多CarSketch's
  2. CarSketch包含一个演示文稿的外键。 CarSketch下可能有两行引用一个演示文稿。
  3. 可以有任意数量的演示记录。
  4. 买方包含一个演示文稿的外键。买家也可能有2行引用一个演示文稿。
  5. CarSketch和买方有外国人参考汽车。
  6.   

    CAR-> CarSketch->演示
      小车 - >与买方>演示

    我如何表达(协会)与CarSketch和买方的演示?!?

    这是对的吗?

    class CarSketch < ActiveRecord::Base    
        belongs_to :presentation
    end
    
    class Buyer < ActiveRecord::Base
        belongs_to :presentation
    end
    
    #what to do here?
    class Presentation < ActiveRecord::Base
        has_many :car_sketches
        has_many :buyers
    end
    

    我不关心在Presentation上创建的记录,真的。

    我希望能够拥有:

    #assume presentation is a Presentation record...
    
    CarSketch.presentation = defined
    Buyer.presentation = some other presentation
    
    also, CarSketch and Buyer could point to the same Presentation record.
    

    非常感谢帮助!

    就个人而言,即使现在有效。当我阅读它时,它没有意义...... CarSketch和买方是我创建的联接表...因为我需要为它们保留更多属性...

    CarSketch 属于对于演示文稿没有意义,因为任何数量的CarSketch都可以有一个演示文稿而演示文稿没有很多CarSketch 看看是什么我在说什么?

    或者将您自己的连接表定义为模型并引用其他模型时,这样会不会感到困惑?

    我对使用Presentation直接检索CarSketch或Buyer不感兴趣。

    更新

    重新编辑,用相应的名称替换A B C D.

    OP,请检查。

    UPDATE*
    
    # A = Car
    # B = CarSketch
    # C = Presentation
    
    # D = Buyer
    
    # 1) a Car can have many car_sketches.
    # 2) a CarSketch has one Presentation.
    # 3) a Car can also have many buyers.
    # 4) a Buyer has one Presentation.
    
    # Car.car_sketches[0].Presentation = some presentation...
    # Car.buyers[0].Presentation = some presentation...
    
    # CarSketch and Buyer can reference the same Presentation record. Different presentation records, too. What would you define under the Presentation model? T
    

1 个答案:

答案 0 :(得分:0)

不是答案,但您可以对此进行扩展,看看它是否正确理解。 (更容易理解一个故事:)也许你拿这个并在你的问题中编织商店,然后我删除这个“答案”。

There's a bunch of Cars and a bunch of Buyers.
Then there is a Maserati.
Sally the Sketcher creates a CarSketch for the Maserati.
    Q: So, Maserati can every only have one sketch.
Now Matt the Manager creates a PowerPoint Presentation containing that sketch.
    Q: So, a presentation can only be for one sketch.
Now, Matt shows Richie Rich the PowerPoint presentation. Richie likes (and is linked to that presentation)
    Q: Now there could be many more buyers linked to that presentation (they all have been shown the presentation)