Rails newb - 你能用我的模特帮我吗?

时间:2012-10-12 05:03:37

标签: ruby-on-rails associations models

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

enter image description here

我的模特对于我想要完成的事情是否正确? 如果没有,有人可以提出一些建议或建议吗? 有没有更简单的方法来做我想做的事情? 从工作流的角度来看,这是否是初学者在缺乏可靠语法的情况下开始的有效方式?

谢谢!

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

一般回答我在你的问题下面的评论:

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

在你的情况下: quote_requests属于customers。 因此,您可以通过quote_requestscustomer_id内引荐客户。

这就够了。通过这种方式,您可以拥有属于客户的零个,一个或多个quote_requests

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

相关问题