내가 올바르게 이해하면 게시물과 게시물이 비디오 또는 사진이 될 수 있습니다. Jaryl이 말한 것처럼 아마도 이해하기 쉽습니다. 그러나 당신이 단일 테이블 상속이나 polymophic 연관을 사용할 수있는 것을 원한다면 다루는 것이 가장 쉽습니다.
STI - 예 (애자일 웹 개발에서 레일과 3 판) 그래서 당신은 당신이 다음 사람을 통해 찾을 수 있습니다
Customer.create(:name => 'John Doe', :email => '[email protected]', :balance => 78.29)
고객을 만들 경우
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
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에 의해 그들 모두를 찾을 수 있지만,
이를 잊지 마세요 (당신은 사진이나 동영상하지 않은 게시물이 경우 포스트 객체)는 사진 및 비디오 객체를 다시 얻을 것 문자열 : db 테이블에 입력하십시오.
http://stackoverflow.com/questions/3231889/rails-sti-with-inheriting-children 저는 STI 자식 개체를 갖는 방법을 알아 내려고하고 있습니다. 따라서 주어진 예제에서는 어떻게 " Person belongs_to : company "및"Company has_many : persons "? – Rabbott