2010-01-12 20 views
9

के माध्यम से मैं किस हद तक मैं रेल में संघों का उपयोग कर सकते करने के लिए सोच रहा हूँ।रेल has_many: has_many के माध्यम से:

class User < ActiveRecord::Base 
    has_one :provider 
    has_many :businesses, :through => :provider 
end 

class Provider < ActiveRecord::Base 
    has_many :businesses 
    has_many :bids, :through => :businesses 
    belongs_to :user 
end 

class Business < ActiveRecord::Base 
    has_many :bids 
    belongs_to :provider 
end 

class Bid < ActiveRecord::Base 
    belongs_to :business 
end 

मैं User.bids की तरह कुछ करने के बारे में User.businesses और Provider.bids की तरह इन गंधा शॉर्टकट लेकिन क्या स्थापित करने के लिए कर रहा हूँ: ध्यान में निम्नलिखित ले लो? क्या एक एसोसिएशन को जोड़ना संभव है, तो बोलने के लिए?

उत्तर

5

यह पूरी तरह संभव है, लेकिन कुछ अतिरिक्त कार्यों की जरूरत है। निम्नलिखित मॉडल nested_has_many plugin साथ संयोजन के रूप में इस्तेमाल किया परिभाषाएँ तुम सिर्फ @user.bids

class User < ActiveRecord::Base 
    has_one :provider 
    has_many :businesses, :through => :provider 
    has_many :bids, :through => :businesses 
end 

class Provider < ActiveRecord::Base 
    has_many :businesses 
    has_many :bids, :through => :businesses 
    belongs_to :user 
end 

class Business < ActiveRecord::Base 
    has_many :bids 
    belongs_to :provider 
end 

class Bid < ActiveRecord::Base 
    belongs_to :business 
end 

हालांकि एक बोली से एक उपयोगकर्ता के लिए हो रही एक और अधिक काम ले जाएगा के साथ एक उपयोगकर्ता से संबंधित सभी बोलियों को प्राप्त कर सके।

+2

यह संभव है, लेकिन क्योंकि आप नीचे अपने डेटाबेस दलदल और एप्लिकेशन रेल कर सकते हैं, के बारे में कितनी गहराई तक आप घोंसला सावधान रहने की जरूरत है। ऐसा कहा जा रहा है कि, मैंने एक ब्लॉग पोस्ट लिखा है जो विवरण करने के लिए nested_has_many_through का उपयोग कैसे करता है: http://kconrails.com/2010/01/28/nesting-has_many-through-relationships-in-ruby-on-rails/ –

2

हालांकि यह है करने के लिए एक बहुत ही उपयोगी चीज है, तो आप has_many नहीं कर सकते: एक has_many के माध्यम से: रिश्ते के माध्यम से। यह जुड़ने इंजन की एक सीमा है।

विकल्प एक चतुर उप का चयन करें, या इस मामले में एक सब-सब चुनिंदा उपयोग करने के लिए, या जानबूझकर काफी टेबल denormalize को गहराई में शामिल होने के कम करने के लिए या तो कर रहे हैं।

उदाहरण के लिए, के बाद से एक व्यापार एक प्रदाता के संदर्भ में परिभाषित किया गया है, यह कारण है कि किसी भी बोली तत्वों को भी आवंटित कर रहे हैं, परोक्ष रूप से, एक प्रदाता के लिए खड़ा है। बोली और प्रदाता के बीच सीधा संबंध बनाने से पूछताछ की बोलियां सीधे आसान हो जाएंगी।

0

कुछ भी नहीं रोक तो आप इस afaik की तरह कुछ कर रही है:

class User < ActiveRecord::Base 
    has_one :provider 
    has_many :businesses, :through => :provider 

    def bids 
     user_bids = [] 
     businesses.each |business| do 
      user_bids += business.bids 
     end 
     user_bids 
    end 
end 

class Provider < ActiveRecord::Base 
    has_many :businesses 
    has_many :bids, :through => :businesses 
    belongs_to :user 
end 

class Business < ActiveRecord::Base 
    has_many :bids 
    belongs_to :provider 
end 

class Bid < ActiveRecord::Base 
    belongs_to :business 
end 

फिर बुला @ user.bids वांछित परिणाम उपज चाहिए, आप भी बोलियां कैश और अन्य फैंसी सामान करना अगर तुम चाहते हो सकता है।

+0

'बोलियां' कैसे कैश की जाएंगी क्योंकि यह गैर स्थैतिक डेटा है? कैश को अपडेट करने के लिए हम कैसे जानते हैं? –

4

यदि आप सिर्फ रिकॉर्ड्स प्राप्त करना चाहते हैं, तो #delegate का उपयोग क्यों न करें? यह आपके द्वारा वर्णित परिदृश्य में कम से कम ठीक काम करता है।

class User < ActiveRecord::Base 
    has_one :provider 
    delegates :bids, :to => :provider 
end 

class Provider < ActiveRecord::Base 
    has_many :businesses 
    has_many :bids, :through => :businesses 
    belongs_to :user 
end 

class Business < ActiveRecord::Base 
    has_many :bids 
    belongs_to :provider 
end 

class Bid < ActiveRecord::Base 
    belongs_to :business 
end 

मेरी नहीं-तो-विनम्र-राय में आप सिर्फ तरीकों श्रृंखला हालांकि चाहिए क्योंकि यह और अधिक स्पष्ट है, और आप प्रदर्शन को बढ़ावा देने को प्राप्त करने के रूप में tadman का कहना है जब तक आप कुछ पागल कस्टम एसक्यूएल के साथ जाना नहीं रह रहे हैं।

संबंधित मुद्दे