Rails 3 STI:删除子类中的关联

时间:2013-06-17 15:19:23

标签: ruby-on-rails-3 associations sti

所以我目前正在开发一个新版本的JSON API,它需要向后兼容。我设置了以下模型:

class Student
  # this is a student of type "version 1"
  has_one :student_information
  has_one :family_information
  #...
end

class V2::Student < ::Student
  # this is a student of type "version 2"
  # which accesses the same table as the version 1
  self.table_name = 'students'
end

所以,所有的关联都是继承的,这很好。但是学生的第2版被定义为family_information被删除的方式。为了与API版本保持兼容,我无法将其从Student基类中删除,但希望将其从V2::Student类中删除。

我怎样才能做到这一点?这甚至是必要的吗?更好的解决方案?

1 个答案:

答案 0 :(得分:0)

您也可以像V2一样制作一个V1,并将关联放在V1中,而不是直接在Student类中。

class Student
  #...
end

class V1::Student < ::Student
  # this is a student of type "version 1"
  has_one :student_information
  has_one :family_information
  self.table_name = 'students'
end

class V2::Student < ::Student
  # this is a student of type "version 2"
  # which accesses the same table as the version 1
  self.table_name = 'students'
end