The following work, but is the Cleanest approach?
简言之,一个项目:大任务。 任务以科学、技术和革新司为代表,任务的不同次类别是:模块(和子类):
# schema
create_table projects do |t|
# t.integer :id - implicitly generated
end
create_table task_bases do |t|
# t.integer :id - implicitly generated
t.string :type # for STI support
t.integer :project_id
end
# file: app/models/project.rb
class Project < ActiveRecord::Base
:has_many :tasks, :class_name => "Task::Base", :dependent => :destroy
end
# file: app/models/task.rb
module Task
def self.table_name_prefix
"task_"
end
end
# file: app/models/task/base.rb
module Task
class Base < ActiveRecord::Base
:belongs_to :project
end
end
# file app/models/task/priority.rb
module Task
class Priority < Base
:belongs_to :project
end
end
几个问题:
- Task::Base should never be instantiated -- we ll only use sub-classes of it. I think this means that I declare it as an abstract_class -- what s the proper way to do that?
- Would it be better to name the STI table
tasks
rather thantask_bases
, and if so, what other changes need to happen? - Should I be troubled by the
:class_name => "Task::Base"
declaration?