2008-11-21 20 views
47

रेल के लिए अपेक्षाकृत नया है और एक व्यक्ति, लिंग, पिता_आईडी और mother_id (2 माता-पिता) वाले एक व्यक्ति मॉडल के साथ एक बहुत ही सरल परिवार "पेड़" मॉडल करने की कोशिश कर रहा है। नीचे मूल रूप से मैं क्या करना चाहता हूं, लेकिन स्पष्ट रूप से मैं दोहराना नहीं कर सकता: एक है_मनी में बच्चों (पहला अधिलेखित हो जाता है)।रेल मॉडल में कई विदेशी_की

class Person < ActiveRecord::Base 
    belongs_to :father, :class_name => 'Person' 
    belongs_to :mother, :class_name => 'Person' 
    has_many :children, :class_name => 'Person', :foreign_key => 'mother_id' 
    has_many :children, :class_name => 'Person', :foreign_key => 'father_id' 
end 

2 विदेशी कुंजी के साथ has_many का उपयोग करें, या हो सकता है परिवर्तित करने के लिए वस्तु के लिंग के आधार पर विदेशी कुंजी एक आसान तरीका है? या फिर एक और बेहतर तरीका है?

धन्यवाद!

+0

रेल 3, गुंजाइश chainning, ActiveRecord :: रिलेशन के लिए और अंत में 'has_many': http://stackoverflow.com/questions/17476521/rails-has-many-custom-activerecord-association/17476639#17476639 – MrYoshiji

+0

आप कर रहे हैं "कंपोजिट कुंजी" की तलाश में: http: // stackoverflow।कॉम/प्रश्न/17882105/यह-संभव-से-परिभाषित-समग्र-प्राथमिक-कुंजी-तालिका-उपयोग-सक्रिय-रिकॉर्ड – xpepermint

उत्तर

43

मिला (रडार के लिए धन्यवाद) काम करने के लिए लगता है कि आईआरसी पर एक साधारण जवाब:

class Person < ActiveRecord::Base 
    belongs_to :father, :class_name => 'Person' 
    belongs_to :mother, :class_name => 'Person' 
    has_many :children_of_father, :class_name => 'Person', :foreign_key => 'father_id' 
    has_many :children_of_mother, :class_name => 'Person', :foreign_key => 'mother_id' 
    def children 
    children_of_mother + children_of_father 
    end 
end 
+20

लेकिन #children एक ऐरे लौटाता है ... –

+1

यदि कोई डिफ़ॉल्ट स्कॉप्स हैं, विशेष रूप से, जो परिणाम के क्रम को प्रभावित कर सकते हैं, यह समाधान काम नहीं करेगा क्योंकि परिणाम अपेक्षित के रूप में आदेश नहीं दिए जाएंगे। – mgadda

+0

यह काम करता है। धन्यवाद! – Abram

8

मेरा मानना ​​है कि आप उन रिश्तों को प्राप्त कर सकते हैं जिनका आप उपयोग करना चाहते हैं: has_one।

class Person < ActiveRecord::Base 
    has_one :father, :class_name => 'Person', :foreign_key => 'father_id' 
    has_one :mother, :class_name => 'Person', :foreign_key => 'mother_id' 
    has_many :children, :class_name => 'Person' 
end 

मैं काम के बाद इस उत्तर की पुष्टि और संपादन करूंगा;)

+0

यह मेरे लिए काम नहीं करता ... सच होना अच्छा लगता था, लेकिन मुझे 'has_many' रिश्ते के लिए अपेक्षित गलती मिली है:' तालिका 'लोगों' में 'person_id' नामक कोई कॉलम नहीं। – deivid

8

प्रयुक्त व्यक्ति मॉडल पर named_scopes ऐसा करते हैं:

class Person < ActiveRecord::Base 

    def children 
     Person.with_parent(id) 
    end 

    named_scope :with_parent, lambda{ |pid| 

     { :conditions=>["father_id = ? or mother_id=?", pid, pid]} 
    } 
end 
4

मैं कार्यक्षेत्रों का उपयोग करना पसंद इस मुद्दे के लिए। इस तरह:

class Person < ActiveRecord::Base 
    belongs_to :father, :class_name => 'Person' 
    belongs_to :mother, :class_name => 'Person' 
    has_many :children_of_father, :class_name => 'Person', :foreign_key => 'father_id' 
    has_many :children_of_mother, :class_name => 'Person', :foreign_key => 'mother_id' 

    scope :children_for, lambda {|father_id, mother_id| where('father_id = ? AND mother_id = ?', father_id, mother_id) } 
end 

यह चाल यह आसान के बिना उपयोग उदाहरणों बच्चों को पाने के लिए करते हैं:

Person.children_for father_id, mother_id 
17

Kenzie के जवाब पर सुधार करने के लिए, आप एक ActiveRecord रिलेशन प्राप्त कर सकते हैं की तरह Person#children को परिभाषित करते हुए:

def children 
    children_of_mother.merge(children_of_father) 
end 

अधिक जानकारी के लिए this answer देख

+0

चेतावनी: जैसा कि आपने उल्लेख किया है, उसमें समझाया गया है, संबंधों को 'AND' के साथ विलय कर दिया गया है। जो इस उदाहरण के साथ काम नहीं करता है, क्योंकि इसका मतलब है कि आप केवल उन लोगों का चयन करते हैं जिनके पास 'mother_id' * और * (* या * के बजाय) 'पिता_आईडी' लक्ष्य की आईडी पर सेट है। मैं डॉक्टर नहीं हूं लेकिन यह अक्सर ऐसा नहीं होना चाहिए :) –

+0

.merge मेरे मामले में काम करता था जहां मुझे चाहिए और। धन्यवाद! – Vlad

3

सामान्य प्रश्न का समाधान नहीं है जैसा कि कहा गया है ("कई विदेशी कुंजी के साथ है_मनी"), लेकिन, एक व्यक्ति को या तो मां या पिता हो सकता है, लेकिन दोनों नहीं, मैं gender कॉलम जोड़ूंगा और

के साथ जाऊंगा
has_many :children_of_father, :class_name => 'Person', :foreign_key => 'father_id' 
    has_many :children_of_mother, :class_name => 'Person', :foreign_key => 'mother_id' 
    def children 
    gender == "male" ? children_of_father : children_of_mother 
    end 
2

मैं एक ही सुविधा की तलाश में था, अगर आप एक सरणी वापस नहीं करना चाहते हैं लेकिन ActiveRecord::AssociationRelation, तो आप + के बजाय << का उपयोग कर सकते हैं। (See the ActiveRecord documentation)

class Person < ActiveRecord::Base 
    belongs_to :father, :class_name => 'Person' 
    belongs_to :mother, :class_name => 'Person' 

    has_many :children_of_father, :class_name => 'Person', :foreign_key => 'father_id' 
    has_many :children_of_mother, :class_name => 'Person', :foreign_key => 'mother_id' 

    def children 
    children_of_mother << children_of_father 
    end 
end 
2

Associations and (multiple) foreign keys in rails (3.2) : how to describe them in the model, and write up migrations के लिए मेरा जवाब केवल आपके लिए है!

अपने कोड के रूप में, यहाँ मेरी संशोधनों

class Person < ActiveRecord::Base 
    belongs_to :father, :class_name => 'Person' 
    belongs_to :mother, :class_name => 'Person' 
    has_many :children, ->(person) { unscope(where: :person_id).where("father_id = ? OR mother_id = ?", person.id, person.id) }, class_name: 'Person' 
end 

इसलिए किसी भी सवाल कर रहे हैं?

+0

यह कमाल है! StackOverflow पर प्रश्न के कई गलत जवाब हैं, लेकिन यह पूरी तरह से काम करता है। त्वरित सुधार हालांकि: आपने 'mother_id' गलत वर्तनी की है। – KurtPreston

+0

आपके समर्थन के लिए धन्यवाद। मैं इसे अभी संपादित कर रहा हूं। – sunsoft

+0

इस उत्तर में मेरी मदद की, यह सही होना चाहिए ... –

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