Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails newb-你能帮我做模型吗?_Ruby On Rails_Associations_Models - Fatal编程技术网

Ruby on rails Rails newb-你能帮我做模型吗?

Ruby on rails Rails newb-你能帮我做模型吗?,ruby-on-rails,associations,models,Ruby On Rails,Associations,Models,这里的第一个问题是Ruby的新问题,所以请对我放轻松。在我开始之前,我试图为我的第一个应用程序绘制一些模型+关联图,以确保我理解了要构建的基本概念 我的模型是否符合我的目标? 如果没有,有人能提供一些建议吗? 有没有更简单的方法来做我想做的事? 从工作流的角度来看,对于初学者来说,这是一种在缺乏可靠语法的情况下快速入门的有效方法吗 谢谢 试试这个 class User < ActiveRecord::Base has_many :customers has_many :provid

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

我的模型是否符合我的目标? 如果没有,有人能提供一些建议吗? 有没有更简单的方法来做我想做的事? 从工作流的角度来看,对于初学者来说,这是一种在缺乏可靠语法的情况下快速入门的有效方法吗

谢谢

试试这个

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
试试这个

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:n关系,则不会引用父表中的所有n个元素。您只定义子表所属的1

就你而言: 报价请求属于客户。 因此,您可以通过客户id在报价请求中引用客户

这就够了。这样,您可以拥有属于客户的零、一或多个报价单请求


告诉自己这样一个问题:如果您没有或有多个条目,quote_request_id有什么用处?如果您将其设置为零,那么这是指id还是表示未分配任何元素?

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

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

就你而言: 报价请求属于客户。 因此,您可以通过客户id在报价请求中引用客户

这就够了。这样,您可以拥有属于客户的零、一或多个报价单请求


告诉自己这样一个问题:如果您没有或有多个条目,quote_request_id有什么用处?如果将其设置为零,那么这是引用id还是意味着未分配任何元素?

客户:它有许多报价请求,为什么通过报价请求id引用一个报价?这同样适用于提供者中的报价单id。我想你可以“看到”它,因为你有两个x两个模型,在两个方向上都有参考客户/quote_请求和提供商/quotesNippey-感谢你的反馈…我理解你的问题,但不太知道如何回答。在customers中,我应该将单个quote_request_id更改为什么,或者如何显示多个关联?从客户/供应商模型中删除quote_request_id和quote_id可以吗,如果我理解正确,我会删除quote_request_id和quote_id,因为您可以从表quote_requests中选择具有正确客户id的quote_请求。您不需要上述id。任何人回答这一问题都会对您有帮助,然后接受回答客户:它有许多quote_请求,为什么您通过quote\u request\u id引用一个quote?这同样适用于提供者中的报价单id。我想你可以“看到”它,因为你有两个x两个模型,在两个方向上都有参考客户/quote_请求和提供商/quotesNippey-感谢你的反馈…我理解你的问题,但不太知道如何回答。在customers中,我应该将单个quote_request_id更改为什么,或者如何显示多个关联?从客户/供应商模型中删除quote_request_id和quote_id可以吗,如果我理解正确,我会删除quote_request_id和quote_id,因为您可以从表quote_requests中选择那些具有正确客户id的quote_请求。那么您不需要上述id。任何对您有帮助的人都可以回答,然后接受回答-谢谢!如果你有另一个简短的第二,你能分享一下我为什么不使用has_many_和属于这里的吗?我猜你创建的其他模型满足了我的意图,并且使它变得不必要……我感到非常尴尬,因为我把关联的语法弄错了……我的意思是“has_and_belies_to_many”,而不是在我的脑子里翻来翻去ohas_many-through比has_many_和_-through更好,因为看到这个链接-12 Dipak-谢谢!如果你有另一个简短的第二,你能分享一下我为什么不使用has_many_和属于这里的吗?我猜你创建的其他模型满足了我的意图,并且使它变得不必要……我感到非常尴尬,因为我把关联的语法弄错了……我的意思是“has_and_belies_to_many”,而不是在我的脑子里翻来翻去ohas_many-through比has_many_和_-through更好,因为看到这个链接-1 2