7

का उपयोग कर रेल 3 जटिल संघों में मैं एक फिल्म आधारित रेल अनुप्रयोग विकसित करने की कोशिश कर रहा हूं जिसमें एकाधिक क्षेत्रों (हॉलीवुड, बॉलीवुड इत्यादि) का समर्थन है। मैं कई क्षेत्रों को आवेदन में भाषाओं के रूप में बुलाता हूं।नेस्टेड_has_many_through

प्रत्येक भाषा का अपना डेटा सेट होता है यानी, अंग्रेजी में हॉलीवुड से संबंधित सभी फिल्में हैं और भाषा हिंदी में बॉलीवुड से संबंधित सभी फिल्में हैं।

भाषा मॉडल

class Language < ActiveRecord::Base 
    has_many :movies 
    has_many :cast_and_crews, :through => :movies, :uniq => true 
    has_many :celebrities, :through => :cast_and_crews, :uniq => true 

    # FIXME: Articles for celebrities and movies 
    has_many :article_associations, :through => :celebrities 
    has_many :articles, :through => :article_associations, :uniq => true 
end 

यहाँ फिल्मों और मशहूर हस्तियों दोनों article_association वर्ग का उपयोग कर लेख है।

मूवी मॉडल

class Movie < ActiveRecord::Base 
    belongs_to :language 
    has_many :cast_and_crews 
    has_many :celebrities, :through => :cast_and_crews 
    has_many :article_associations 
    has_many :articles, :through => :article_associations, :uniq => true 
end 

सेलिब्रिटी मॉडल

class Celebrity < ActiveRecord::Base 
    has_many :cast_and_crews 
    has_many :movies, :through => :cast_and_crews, :uniq => true 
    has_many :article_associations 
    has_many :articles, :through => :article_associations, :uniq => true 
end 

class ArticleAssociation < ActiveRecord::Base 
    belongs_to :article 
    belongs_to :celebrity 
    belongs_to :movie 
end 

और यह कैसे मेरी अनुच्छेद मॉडल परिभाषित किया गया है है

class Article < ActiveRecord::Base 
    has_many :article_associations 
    has_many :celebrities, :through => :article_associations 
    has_many :movies, :through => :article_associations 
end 

जो मैं हासिल करने की कोशिश कर रहा हूं वह भाषा है। कार्टिकल को हस्तियों और फिल्मों से संबंधित सभी लेख वापस करना चाहिए।

कारण मैं SQL का उपयोग नहीं कर रहा हूं find_by_sql ActiveRelation का समर्थन नहीं करता है और मैं has_scope कार्यक्षमता का उपयोग नहीं कर पाऊंगा।

मैं nested_has_many_through, has_scope और inherited_resources जवाहरात

किसी भी मदद इस में बहुत सराहना की जाएगी उपयोग कर रहा हूँ।

+0

एक तरफ ध्यान दें के रूप में, क्षेत्र का प्रतिनिधित्व करने वाले के रूप में भाषा एक सा जटिल है। क्या होगा अगर एक भारतीय फिल्म अंग्रेजी में है? अवधारणाओं को विभाजित करें। –

+0

अगर मैं दीवार समझता हूं, तो आपकी परेशानी नेस्टेड है_मनी से नहीं आती है: इसके माध्यम से (आपके पास यह मणि है), लेकिन इस तथ्य से कि आप 2 स्रोतों (फिल्मों और हस्तियों) से लेख प्राप्त करना चाहते हैं? क्या आपने समस्या को उलट करने का प्रयास किया है? भाषा में has_many संबंध परिभाषित नहीं है, लेकिन अनुच्छेद में एक लैम्ब्डा गुंजाइश परिभाषित? हालांकि इसमें कुछ एसक्यूएल शामिल हो सकता है। – MrRuru

+0

@MrRuru आप सही हैं। मुझे nested_has_many_through मणि ​​के साथ कोई समस्या नहीं है। यह वही करता है जो वादा करता है। इसके अलावा आप सही हैं कि मेरे पास लेखों के लिए कई स्रोत हैं, जैसे फिल्में और हस्तियां। मैं एसक्यूएल आधारित स्कोप से बचने की कोशिश कर रहा हूं क्योंकि एसक्यूएल आधारित गुंजाइश मुझे एक सक्रिय रिलेशनशिप इंस्टेंस नहीं लौटाती है और मैं उन अन्य स्कोपों ​​के लिए आवश्यक स्कॉप्स को चेन करने में सक्षम नहीं हूं जिन्हें मैं i.e., विरासत_resources का उपयोग करता हूं। सुझाव के लिए –

उत्तर

0

ठीक है, मैंने इसे ठीक करने के लिए किया है।

जोड़ा गया मेरी अनुच्छेद कक्षा में निम्नलिखित गुंजाइश

def self.region(region_id) 
    joins(<<-eos 
    INNER JOIN 
    (
     SELECT DISTINCT aa.article_id 
     FROM regions r 
      LEFT JOIN movies m on m.region_id = r.id 
      LEFT JOIN cast_and_crews cc on cc.movie_id = m.id 
      LEFT JOIN celebrities c on c.id = cc.celebrity_id 
      LEFT JOIN events e on e.region_id = r.id 
      LEFT JOIN article_associations aa on (aa.event_id = e.id or aa.movie_id = m.id or aa.celebrity_id = c.id) 
     WHERE r.id = #{region_id} 
    ) aa 
    eos 
).where("aa.article_id = articles.id") 
end 

यह मैं एक ActiveRecord :: रिलेशन उदाहरण है कि मैं उम्मीद कर रहा हूँ जो एक फिल्म, सेलिब्रिटी या घटना के लिए सभी रिकॉर्ड को पुन: प्राप्त देता है।

उन सभी के लिए धन्यवाद जिन्होंने मेरी मदद की।

यदि आपके पास इसे सुधारने के लिए कोई टिप्पणी है तो कृपया टिप्पणी करें। बहुत ज्यादा अधिमूल्यित।

1

कुछ चाल है कि अनुमति चाहिए आपको क्या चाहिए, Article से बाहर जा रहा दी भाषा आईडी के लिए सभी Movies क्वेरी कर सकते हैं

class Article < ActiveRecord::Base 
    has_many :article_associations 
    has_many :celebrities, :through => :article_associations 
    has_many :article_movies, :through => :article_associations, :class => 'Movie' 
    scope :for_language, lambda {|lang_id| 
    joins(
     :article_associations=>[ 
     :article_movies, 
     {:celebrities => { :cast_and_crews => :movies } } 
     ] 
    ).where(
     'movies.language_id = ? OR article_movies.language_id = ?', 
     lang_id, lang_id 
    ) 
    } 
end 

फिर भाषा में एक विधि है कि Article

के पूर्व गुंजाइश का उपयोग करेगा परिभाषित
class Language < ActiveRecord::Base 
    has_many :movies 
    has_many :cast_and_crews, :through => :movies, :uniq => true 
    has_many :celebrities, :through => :cast_and_crews, :uniq => true 

    def articles 
    Article.for_language id 
    end 
end 

केवल अनिश्चित यहाँ हिस्सा कैसे :article_movies एसक्यूएल में प्रतिनिधित्व किया जाएगा है ...

+0

। मैंने जो सुझाव दिया था, मैंने कोशिश की लेकिन मुझे निम्न त्रुटि मिलती है जब मैं "ActiveRecord :: ConfigurationError: एसोसिएशन नाम 'article_movies' नहीं मिला था, शायद आपने इसे गलत वर्तनी दी थी? "। मुझे लगता है कि यह हो रहा है क्योंकि मेरे पास article_association class –

+0

में article_movies नहीं है, आपको ArticleAssociation में 'belong_to: article_movie,: class =>' मूवी 'का प्रयास करना चाहिए, लेकिन इसके लिए माइग्रेशन की आवश्यकता होगी (' movie_id' से 'article_movie_id' में कुंजी बदलना होगा - मुझे लगता है। – mpapis

1

रेल 3.1 अब घोंसले संबंधों के लिए समर्थन है।बेशक एक में बनाया बेहतर एक प्लगइन तो होना चाहिए :)

http://railscasts.com/episodes/265-rails-3-1-overview

+0

हां आप सही मोहम्मद हैं। मुझे पता है कि रेल 3.1 ने नेस्टेड एसोसिएशन किया है। मुझे पता है कि मैं इस प्लगइन का बहुत लंबे समय तक उपयोग नहीं करूँगा। –