2

ここでの最初の質問で、Rubyは初めてなので、気楽にやってください。構築する基本的な概念を理解していることを確認するために、始める前に、最初のアプリのいくつかのモデルと関連付けを図解しようとしました。

ここに画像の説明を入力してください

私のモデルは、私が達成しようとしていることに対して正しいですか?そうでない場合、誰かがいくつかの提案や推奨事項を提供できますか?私がやろうとしていることを行うためのより簡単な方法はありますか?ワークフローの観点から、これは初心者がしっかりした構文がなくても始めるための効率的な方法ですか?

ありがとう!

4

2 に答える 2

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 に答える
0

あなたの質問の下にある私のコメントへの一般的な回答:

リレーションを持つ 2 つのテーブルがある場合、親テーブルの1:nすべての要素を参照するわけではありません。子テーブルが属するnものを定義するだけです。1

あなたの場合: quote_requestsに属していcustomersます。したがって、quote_requestsvia内で顧客を参照しますcustomer_id

それは十分です。quote_requestsこのようにして、顧客に属するゼロ、1 つ、または複数を持つことができます。

(次の質問を自問してください: ゼロまたは複数のエントリがある場合、 はどのような用途に使用quote_request_idされるでしょうか? ゼロに設定すると、これは ID を参照しますか、それとも要素が割り当てられていないことを意味しますか?)

于 2012-10-12T05:36:39.833 に答える