class Contest < ActiveRecord::Base
has_one :claim_template
end
class ClaimTemplate
include Mongoid::Document
belongs_to :contest
end
# console
Contest.new.claim_template
#=> NoMethodError: undefined method `quoted_table_name' for ClaimTemplate:Class
OK、のはClaimTemplate
にquoted_table_name
を追加してみましょう。MongoidとActiveRecordの関係:未定義のメソッド `quoted_table_name」
def self.quoted_table_name
"claim_templates"
end
# console
Contest.new.claim_template
#=> nil
# Cool!
# But:
Contest.last.claim_template
#=> TypeError: can't convert Symbol into String
それでは、どのように私は私のモデルが互いに
PSで正しく動作するように設定することができます:
今私はうまく動作するこの構造を持っていますが、私はR寛解(Assosiations
)。
class Contest < ActiveRecord::Base
# has_one :claim_temlate
def claim_template
ClaimTemplate.where(:contest_id => self.id).first
end
# Mongoid going to be crazy without this hack
def self.using_object_ids?
false
end
end
何「関係のメリットは、」あなたが望んでいますか? ActiveRecordのアソシエーションは、アソシエーション内の両方のモデルがActiveRecordオブジェクトであるという前提の下で動作するため、 'has_one'と' belongs_to'の動作をまとめて取得することはほとんどありません。 –
@Michael Fairley、コンテストを通じて新しいCleimTemplateを作成し、ネストされたフォームを作成し、 'accepts_nested_attributes_for 'などのように' Associations'メソッドを使用したいと思います。 'Mongoid'と' ActiveRecord'が単なる抽象であり、彼らは簡単にこの振る舞いを扱うことができる限り、それらの両方を一緒に使用するのに問題はありません。 – fl00r
@ fl00r MongoidとActiveRecordは抽象であり、 ActiveModelですが、彼らが全く同じAPIを公開し、リレーションのメタデータを互換性のある方法で保存しない限り(AFAIKはそうではありません)、私はあなたがこの種のクロスオーバー関係を働かせることはできませんコースを書き直すことなく)。 – brahmana