Se ho capito bene hai post e messaggi possono essere video o foto. come Jaryl ha detto che quello che hai è probabilmente il più facile da capire/gestire, tuttavia se volessi avere fantasia potresti usare l'ereditarietà di una singola tabella o associazioni polimorfiche.
STI - Esempio (da Agile Web Development con 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
Quindi, se si crea un cliente
Customer.create(:name => 'John Doe', :email => '[email protected]', :balance => 78.29)
si può quindi trovare tramite persona
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
Quindi potresti avere
class Post < ActiveRecord::Base
end
class Photo < Post
end
class Video < Post
end
e poi li potrebbe trovare da POST.All ma si sarebbe tornare Foto e Video oggetti (e oggetti postali se si dispone di messaggi che non sono foto o video)
non dimenticare le stringa: digitare nella tabella db
http://stackoverflow.com/questions/3231889/rails-sti-with-inheriting-children Sto cercando di capire come avere lo STI come oggetto figlio, quindi con l'esempio fornito come faccio a codificare " La persona appartiene a: società "e" La società ha_molte persone "? – Rabbott