2010-02-14 6 views
5

मान लें कि हमारे पास एक फोटोग्राफी साइट है। कोई भी लेखक किसी अन्य लेखक से अपडेट प्राप्त करने के लिए सदस्यता ले सकता है। जाहिर है अगर लेखक एक लेखक बी की सदस्यता दी गई मतलब यह नहीं है कि कि बी ए की सदस्यता दी गई तो हम मॉडलरेल में "दो तरफ" कई से अधिक रिश्तों को कैसे बनाया जाए?

class Author < ActiveRecord::Base 
    has_many :subscriptions 
    has_many :subscribed_by_author, :through => :subscriptions, :source => :subscribed_to 
end 

class Subscription < ActiveRecord::Base 
    belongs_to :author 
    belongs_to :subscribed_to, :class_name => "Author", :foreign_key => "subscribed_to" 
end 

इस तरह का निर्माण हम

  1. some_author.subscribed_by_author उपयोग कर सकते हैं - सूची लेखकों के लिए जिनके लिए कुछ_अधिकृत सदस्यता ली गई है।
  2. किसी भी सदस्यता हम दोनों सिरों (कौन किसे की सदस्यता दी गई)

लेकिन सवाल यह है कि कुछ लेखक केवल रेल का उपयोग करने के सदस्यता लेने वाले लोगों की सूची प्राप्त करने के लिए है पता कर सकते हैं के लिए (सादा एसक्यूएल का उपयोग नहीं) यानी प्राप्त इसका उत्तर: "किसने कुछ_अधिकृत की सदस्यता ली है?"

प्रश्न: क्या दोनों पक्षों के रिश्ते को पाने के लिए रेल में कोई क्षमता है यानी some_author.subscribed_BY_author पर न केवल some_author_subscribed_TO_author? यदि कोई है, तो यह क्या है?

पीएस subscribed_BY_author: स्पष्ट समाधान, के लिए

  1. बदलें डेटाबेस डिजाइन है नाम के एक कॉलम शामिल "दिशा"
  2. 2 बनाएं हर बार एक सदस्यता
  3. लेखक मॉडल

    has_many में जोड़े बनाई गई है रिकॉर्ड ,: through =>: सब्सक्रिप्शन, स्रोत =>: subscribed_to,: condition => "direction = 'द्वारा

    has_many: subscribed_TO_author,: through =>: सदस्यता, स्रोत =>: subscribed_to,: condit आयनों => "दिशा = 'करने के लिए' '

लेकिन मैं आश्चर्य है कि अगर डेटाबेस डिजाइन बदले बिना एक समाधान है।

उत्तर

0
# Author model 
has_many :subscriptions_to, :class_name => "Subscription", :foreign_key => "subscribed_to" 
has_many :subscribed_to_author, :through => :subscriptions_to, :source => :author 

जहाँ तक मुझे पता है - यह काम करता है! :)

+0

दुर्भाग्य से ऐसा नहीं हुआ।लेकिन उसने मुझे सही दिशा दी और निम्नलिखित कोड # लेखक मॉडल has_many: subscription_to,: class_name => "सदस्यता",: विदेशी_की => "subscribed_to" has_many: subscribed_to_author,: through =>: subscriptions_to, स्रोत =>: लेखक इसलिए मैं इस उत्तर को स्वीकार करता हूं, लेकिन आपको पहले इसे सही करने के लिए इसे संपादित करना होगा :) धन्यवाद! –

+0

मेरे एवर को ठीक करना हमेशा मेरी बड़ी खुशी है :) – klew

2

मैं इस तरह के कुछ सरल के लिए सादा एचएबीटीएम का उपयोग करता हूं, लेकिन आपको किसी भी मामले में शामिल होने की आवश्यकता नहीं होगी। यह करने के लिए

create_table :subscriptions do |t| 
    t.column :author_id, :integer 
    t.column :subscriber_id, :integer 
end 

प्वाइंट लेखक:

class Author < ActiveRecord::Base 
    has_and_belongs_to_many :subscribers 
    :class_name => "Author", 
    :join_table => "subscriptions", 
    :association_foreign_key => "subscriber_id" 

    def subscriptions # "subscribers" is already included above 
    self.subscribers.find(:all, :subscriber_id=>author.id) # hopefully not too 
    end              # much SQL 
end 

तुम सच में अपने विधि के नाम करने के लिए प्रतिबद्ध है तोः

def subscribed_to_author 
    subscribers 
    end 

    def subscribed_by_author(author) 
    self.subscribers.find(:all, :subscriber_id=>author.id) 
    end 

कुछ कनेक्शन बनाएं (मैं चाहता हूँ SubscriptionsController Resty होने के लिए)

SubscriptionsController < ApplicationController 
    def create 
    @author = Author.find(params[:author_id] # author to be subscribed to 
    @user = current_user # user clicking the "subscribe" button 

    @author.subscribers << @user # assuming authors should only 
    @author.save     # be able to subscribe themselves 
    end 
end 

डिस्प्ले नाम, या जो भी

@author.subscribers.each do |s| 
    s.name 
end 
# ...or...and...also... 
<%= render :partial => @author.subscribers -%> 
<%= render :partial => @author.subscriptions -%> 
+0

धन्यवाद! वह बहुत उपयोगी था। अब मुझे समस्या को हल करने के लिए दो विधियां मिली हैं। –

+0

@ सर्गी, यही वह है जो मैं अपने उत्तर में बात कर रहा था, जिसे मैंने एरिक के तरीके से हटा दिया है। –

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