2009-12-31 3 views
2

현재 데이터 액세스를 위해 DataMapper를 사용하여 기존 데이터베이스를 기반으로하는 새 앱을 만들고 있습니다. 그러나, 외래 키를 다룰 때의 규약은 데이터베이스가 사용하는 규약이 아닙니다.DataMapper 연결의 외래 키 이름

예 :

class Invoice 
    include DataMapper::Resource 

    storage_names[:default] = 'invoices' 

    property :id, Serial 
    # ... more properties ... 

    has n, :items 
end 

class Item 
    include DataMapper::Resource 

    storage_names[:default] = 'invoiceItems' 

    property :id, Serial 
    # ... more properties ... 

    belongs_to :invoice # this should use 'invoiceId' instead of 'invoice_id' 
end 

차라리 'invoice_id'가 아닌 'invoiceId'로 DataMapper에 사용되는 외부 키를 얻을 수있는 방법 주석으로 표시된 바와 같이이 (순간에 사용하려고 있는가 위)? 나는 이것이 일반적인 필드에서 :field => 'fieldName'을 추가하여 수행 할 수 있다는 것을 알고 있지만 연관성을위한 그런 방법을 찾지 못했습니다.

답변

5

첫 번째로는 아니지만 Jan De Poorter의 very helpful blog entry 덕분에 내 자신의 질문에 대한 답을 찾았습니다.

필드 이름을 처리 할 때 규칙을 조정해야 기호의 이름을 밑줄에 관한 내장 스마트 대신 사용하기 만하면됩니다.

는 외래 키 이름은 다음과 같이 지정 될 수
repository(:default).adapter.field_naming_convention = lambda { |value| value.name.to_s } 

다음 : 키 이름 관계의 양쪽을 지정해야한다는 위와 같이

class Invoice 
    # bla bla... 
    has n, :items, :child_key => [:invoiceId] 
end 

class Item 
    # bla bla... 
    belongs_to :invoice, :child_key => [:invoiceId] 
end 

그것은 (주목할 가치가있는 나는 이상하게 보입니다,하지만 헤이). 다행히도 이것은 동일한 질문을하는 다른 사람들을 도울 것입니다. 대답은 Jan De Poorter에게 감사합니다.