यदि मैं आपको सही ढंग से समझता हूं तो आपके पास पोस्ट और पोस्ट या तो वीडियो या फोटो हो सकते हैं। जैसा कि जैरील ने कहा था कि आपके पास जो कुछ है वह शायद समझने/संभालने में सबसे आसान है, हालांकि यदि आप कल्पना करना चाहते हैं तो आप एकल टेबल विरासत या पॉलिमोफिक संघों का उपयोग कर सकते हैं।
एसटीआई - उदाहरण (चंचल वेब विकास से रेल के साथ 3 संस्करण)
create_table :people, :force => true do |t|
t.string :type
#common attributes
t.string :name
t.string :email
#attributes for type=Customer
t.decimal :balance, :precision => 10, :scale => 2
#attributes for type=Employee
t.integer :reports_to
t.integer :dept
#attributes for type=Manager
#none
end
class Person < ActiveRecord::Base
end
class Customer < Person
end
class Employee < Person
belongs_to :boss, :class_name => "Manager", :foreign_key => :reports_to
end
class Manager < Person
end
तो अगर आप एक ग्राहक
Customer.create(:name => 'John Doe', :email => '[email protected]', :balance => 78.29)
आप तो व्यक्ति के माध्यम से इसे प्राप्त कर सकते हैं बनाने के
x = Person.find_by_name('John Doe')
x.class #=> Customer
x.email #=> [email protected]
x.balance #=> 78.29
x.some_customer_class_method # will work because the Person.find method returned a Customer object
तो आपके पास
हो सकता है
class Post < ActiveRecord::Base
end
class Photo < Post
end
class Video < Post
end
और फिर आप उन सब को Post.all से मिल सकता है लेकिन
मत भूलना (यदि आप पदों उस फ़ोटो या वीडियो नहीं हैं और बाद वस्तुओं) आप फोटो और वीडियो वस्तुओं वापस मिल जाएगा स्ट्रिंग: अपनी डीबी तालिका में टाइप करें
http://stackoverflow.com/questions/3231889/rails-sti-with-inheriting-children मैं यह पता लगाने की कोशिश कर रहा हूं कि एसटीआई एक बच्चे वस्तु कैसे है, इसलिए दिए गए उदाहरण के साथ मैं " व्यक्ति belong_to: कंपनी "और" कंपनी है_मेनी: व्यक्तियों "? – Rabbott