2

这里的第一个问题是 Ruby 的新手,所以请放轻松。在开始之前,我尝试为我的第一个应用程序绘制一些模型 + 关联,以确保我理解了可以构建的基本概念。

在此处输入图像描述

我的模型是否适合我想要完成的任务?如果没有,有人可以提供一些建议或建议吗?有没有更简单的方法来做我想做的事情?从工作流程的角度来看,对于初学者来说,这是一种在缺乏扎实语法的情况下入门的有效方式吗?

谢谢!

4

2 回答 2

0

在您的问题下方对我的评论的一般回答:

如果您有两个具有1:n关系的表,则不会引用n父表中的所有元素。您只需定义1子表属于哪个。

在您的情况下: quote_requests属于customers. quote_requests因此,您在via中引用客户customer_id

足够了。这样您就可以拥有quote_requests属于客户的零个、一个或多个。

(告诉自己一个问题:quote_request_id如果你有零或多个条目有什么用?如果你将它设置为零,那么这是否引用了一个 id 或是否意味着没有分配任何元素?)

于 2012-10-12T05:36:39.833 回答
0

尝试这个

class User < ActiveRecord::Base
  has_many :customers
  has_many :providers
end

class Customer < ActiveRecord::Base
  belongs_to :user

  has_many :customer_quote_requests
  has_many :quote_requests, :through => :customer_quote_requests

  has_many :customer_quotes
  has_many :quotes, :through => :customer_quotes
end

class Provider < ActiveRecord::Base
  belongs_to :user

  has_many :provider_quotes
  has_many :quotes, :through => :provider_quotes

  has_many :provider_quote_requests
  has_many :quote_requests, :through => :provider_quote_requests

end

class QuoteRequest < ActiveRecord::Base
  has_many :customer_quote_requests
  has_many :customers :through => :customer_quote_requests

  has_many :provider_quote_requests
  has_many :providers, :through => :provider_quote_requests
end

class CustomerQuoteRequest < ActiveRecord::Base
  belongs_to :customer
  belongs_to :quote_request
end

class Quote < ActiveRecord::Base
  has_many :provider_quotes
  has_many :provider, :through => :provider_quotes

  has_many :customer_quotes
  has_many :customers, :through => :customer_quotes

end

class ProviderQuote < ActiveRecord::Base
  belongs_to :provider
  belongs_to :qoute
end

class ProviderQuoteRequests < ActiveRecord::Base
  belongs_to :provider
  belongs_to :quote_requests
end

class CustomerQuotes < ActiveRecord::Base
  belongs_to :customer
  belongs_to :quote
end
于 2012-10-12T05:24:24.250 回答