1

私はデータベースとリレーショナル ロジックの専門家ではなく、次の状況で何をしなければならないかについて少し混乱しています。

多対多の関係を参照Expressionする自己を実装したいモデルがあります。TranslationPair

class Expression
  include DataMapper::Resource

  has n, :translation_pairs, child_key: [:exp_1_id]
end

class TranslationPair
  include DataMapper::Resource

  belongs_to :exp_1, 'Expression', key: true
  belongs_to :exp_2, 'Expression', key: true
end

問題は、関係がフィールド内の特定の式を持つインスタンスだけでなく、フィールド内の特定の式を持つインスタンスtranslation_pairsも返すことです (が の翻訳である場合、は の翻訳です)。オプションの選言の一種。何かのようなもの:TranslationPairexp_1TranslationPairexp_2expression1expression2expression2expression1child_key

has n, :translation_pairs, child_key: [:exp_1_id] or [:exp_2_id]

モデル宣言で直接実装できますか、それともカスタム メソッドを実装する必要がありますか?

4

1 に答える 1

1

面白い問題!

説明されているようにこれを行う方法はなく、DataMapper コア メソッドだけを使用します。私は今、データの性質について推測しているだけです...しかし、次のように、与えられた任意の「標準的な」表現を思い付くことができるかどうか興味がありますExpression:

class Expression
  belongs_to :canonical_translation
  ...

  def equivalent_expressions
    canonical_translation.expressions.all(:id.not => self.id)
  end
end

class CanonicalTranslation
  property :representation, SomeDataType
  has n :expressions
end

そうでない場合は、次のようなカスタム メソッドを Expression オブジェクトで使用する必要があります。

has n, :these_translations, :model => TranslationPair, :child_key => [:exp_1]
has n, :those_translations, :model => TranslationPair, :child_key => [:exp_2]

def translation_pairs
  these_translations + those_translations
end
于 2013-06-27T22:24:41.977 に答える