Jeśli rozumiem cię poprawnie, masz posty i posty mogą być wideo lub zdjęcia. jak Jaryl powiedział, że to, co masz, jest prawdopodobnie najłatwiejsze do zrozumienia/radzenia sobie, ale jeśli chciałeś mieć ochotę, możesz użyć dziedziczenia pojedynczego stołu lub skojarzeń polimorficznych.
STI - przykład (z Agile Web Development z Rails 3rd Edition)
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
Więc jeśli utworzyć Państwo klientowi
Customer.create(:name => 'John Doe', :email => '[email protected]', :balance => 78.29)
następnie można go znaleźć za pośrednictwem osoby
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
Dzięki temu możesz mieć
class Post < ActiveRecord::Base
end
class Photo < Post
end
class Video < Post
end
a następnie można je wszystkie znaleźć przez Post.all ale wrócisz zdjęć i filmów wideo (obiekty i obiekty post, jeśli masz wiadomości, które nie są zdjęcia lub wideo)
nie zapomnieć ciąg: wpisz w swojej tabeli db
http://stackoverflow.com/questions/3231889/rails-sti-with-inheriting-children Próbuję dowiedzieć się, jak ustawić obiekt STI jako obiekt podrzędny, więc z podanym przykładem, w jaki sposób mogę kodować " Osoba belongs_to: company "and" Company has_many: persons "? – Rabbott