【问题标题】:Rails 3.0 has_many and belongs_to associations headache.Rails 3.0 has_many 和 belongs_to 关联让人头疼。
【发布时间】:2013-06-23 10:20:55
【问题描述】:

目前正在学习 Rails,所以要温柔。

我想在我走远之前检查我是否正确:

构建一个允许我们的“客户”登录并创建报价的应用程序。 “供应商”(在这种情况下为会议场地所有者)然后可以查看报价,并用客户可以查看的建议回复他们。每个供应商帐户都可以选择拥有一个或多个属于它的“场地”(例如,如果他们经营一系列场地),并且提出的每个提案都来自特定场地。

我以后可能会遇到的其他复杂问题,就关系而言,这看起来是否正确?

P.S 我意识到下面的代码实际上并不是可以工作的,我只是这样布置它,同时我试图理解它。

Customer (will be a type of user)
has_many :quotes
has_many :proposals, :through => :venue

Supplier (will be a type of user)
has_many :venues
has_many :proposals, :through => :venue

Venue
belongs_to :supplier 
has_many Proposals

Quote
belongs_to :customer

Proposal
belongs_to :venue

还有基本表:

Customer
    id

Supplier
    id

Quote
    id
    customer_id

Venue
    id
    supplier_id

Proposal
    id
    venue_id

可能有更好的方法来做到这一点,使用 has_ones 和 has_and_belongs_to_many 等,但我不知道。

谢谢

【问题讨论】:

  • 你希望customers如何与venues相关联?因为如果你想让customers 有很多proposalsvenuescustomers 也应该有很多venues
  • 客户可以从任何地点获取建议书——也许我不需要通过的?我可以删除这两个吗?
  • 所以你应该至少从Customer has_many :proposals 中删除:through。您希望您的suppliers 如何与提案相关联?
  • 每个供应商都会有许多提案 - 但每个提案都来自特定地点,每个地点都属于某个供应商。 (我本可以取消“场地”模式,但它使供应商不必为他们拥有的每个场地注册该网站 - 我认为......

标签: ruby-on-rails activerecord associations has-many belongs-to


【解决方案1】:

好的,所以我认为它应该是这样的:

Customer
  has_many :quotes
  has_many :proposals

Supplier
  has_many :venues
  has_many :proposals, :through => :venues

Venue
  belongs_to :supplier 
  has_many :proposals

Quote
  belongs_to :customer

Proposal
  belongs_to :venue
  belongs_to :customer

在 db 架构中:

Customer
  id

Supplier
  id

Quote
  id
  customer_id

Venue
  id
  supplier_id

Proposal
  id
  venue_id
  customer_id

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-11
    • 2015-04-17
    • 1970-01-01
    • 1970-01-01
    • 2022-07-08
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    相关资源
    最近更新 更多