1

이것은 매우 간단 할 것입니다. 그러나 저는 루비와 액티브 레코드가 새롭습니다.CSV to DataMapper 가져 오기

DataMapper를 사용하여 데이터베이스로 가져 오려고하는 데이터베이스의 CSV 덤프가 있습니다. 모델에서 정의해야하는 관계 유형을 이해하여 문제가 정의 된 CSV와 일치하도록해야합니다. 프로젝트 belong_to 단계를 수행하거나 has_many 관계는 다음과 같습니다 예를 들어

Stages: 
id 
staff_id 
project_id 
job_id 
company_id 

Projects: 
id 
company_id 

Jobs: 
id 
project_id 
company_id 

Client: 
id 

Staff: 
id 

:

여기가 CSV에서 가지고있는 데이터입니까?

+1

에 따라 달라집니다? (이들은 서로 다른 두 가지입니다) – mikej

+0

죄송합니다, 나는 DataMapper를 사용하고 있지만 관계 유형이 유사하다고 가정합니까? 예 : 1 대 다수 등 – Tom

답변

1

나는 클라이언트 == 회사라고 가정하고 있습니다. 여기에 여기에

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 

:

  1. Client, Staff 그들이 할 수 있기 때문에 다른 모든 모델과 독립적으로 존재합니다.
  2. Project, 이는에만 의존합니다.
  3. Job
  4. Project
  5. StageClient에 따라 달라집니다 당신이 액티브 또는 DataMapper를 사용하고 Staff, Project, JobClient
+0

고마워요. 외래 키의 개념이 누락 된 것처럼 보였고 client_client_id와 같은 많은 필드가있는 것으로 보입니다. – Tom

관련 문제