我假设客户==公司。这里的ActiveRecord
class Stage < ActiveRecord::Base
belongs_to :staff
belongs_to :project
belongs_to :job
belongs_to :company, :class => "Client"
end
class Project < ActiveRecord::Base
belongs_to :company, :class => "Client"
has_many :stages
end
class Job < ActiveRecord::Base
belongs_to :project
belongs_to :company, :class => "Client"
has_many :stages
end
class Client < ActiveRecord::Base
has_many :jobs, :foreign_key => "company_id"
has_many :projects, :foreign_key => "company_id"
has_many :stages, :foreign_key => "company_id"
end
class Staff < ActiveRecord::Base
has_many :stages
end
这里DataMapper的一个例子为例:
class Stage
include DataMapper::Resource
property :id, Serial
belongs_to :staff
belongs_to :project
belongs_to :job
belongs_to :company, "Client"
end
class Project
include DataMapper::Resource
property :id, Serial
belongs_to :company, "Client"
has n, :stages
end
class Job
include DataMapper::Resource
property :id, Serial
belongs_to :project
belongs_to :company, "Client"
has n, :stages
end
class Client
include DataMapper::Resource
property :id, Serial
has n, :jobs, :foreign_key => "company_id"
has n, :projects, :foreign_key => "company_id"
has n, :stages, :foreign_key => "company_id"
end
class Staff
include DataMapper::Resource
property :id, Serial
has n, :stages
end
对于你应该做一个特殊的顺序导入:
Client
,Staff
,因为他们可以存在独立于所有其他型号
Project
,它仅取决于Client
Job
,取决于Project
和Client
Stage
,您使用的ActiveRecord或者DataMapper的取决于Staff
,Project
,Job
和Client
? (这些是2种不同的东西) – mikej 2010-06-20 18:56:13
对不起,我使用的是DataMapper,但假设关系类型相似?例如一对多等 – Tom 2010-06-20 19:50:22