我假設客戶==公司。這裏的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