-1

私は多くのコントローラーを持っている従業員モデルを持っています

jobs_controller.rb
contacts_controller.rb
personals_controller.rb
dependets_controller.rb

それらはすべて従業員コントローラーに関連しています。私はMongoDbを使用しています。これは、コントローラーが異なるため、モデルも異なるためです。ダッシュボードに、関連する従業員の詳細を表示する必要があります。ここで、1つのフィールドは連絡先コントローラーから、別のフィールドはdependents_controllerから、別のフィールドはパーソナルコントローラーからのものです。ここでは、すべてのモデルを呼び出し、各モデルから1つのフィールドをフェッチする必要があります。1つの関連するモデルの各フィールドを表示して、このコードをカスタマイズできますか?私はデバイスを使用しています。各ユーザー関連データのIDを保存して、ユーザーモデルを呼び出すことはできませんか?私はめちゃくちゃです..もしそうなら、どうやって?私の従業員コントローラーで

def index
    @employees = Employee.all
    Employee.includes(:dependants).each do |dependant|
     p dependant.firstname #example of pulling data for that related entity
     end


  end

また、関係者データを見つけるにはどうすればよいですか?

4

1 に答える 1

2

Mongoid ドキュメントのこのセクションを見てください: http://mongoid.org/en/mongoid/docs/relations.html モデル内にリレーショナル ロジックを実装する方法を示す良い指標となるはずです。

ただし、最初にこれを読むことを強くお勧めします: http://docs.mongodb.org/manual/core/data-modeling

特に、参照セクションと原子性セクション。

私は過去に、mongo とトランザクション DB エンジン (innodb など) の違いを完全に把握していないことに気づき、プロジェクトの途中でモデルを再設計することになりました。

要求に応じて説明

私の最初のリンクで説明されているように、モデルを次のように設定できます。

class Employee
  include Mongoid::Document
  field :address, type: String
  field :work, type: String
  has_many :dependants
end

class Dependant
  include Mongoid::Document
  field :firstname, type: String
  field :lastname, type: String
  belongs_to :employee
end

コントローラーでは、従業員を介して依存データにアクセスできます。

#this example loops through every employee in the collection
Employee.includes(:dependants).each do |employee|
  employee.dependants.each do |dependant|
   p dependant.firstname #example of pulling data for that related entity
  end
end
于 2012-12-24T11:24:54.793 に答える