0

:noteshas_manyと:notebooks:notebookhas_manyを持つ「User」というデバイスモデルがあるとし:notesます。

したがって、メモには2つの外部キーが:user_idあり、メモ:notebook_idを作成/検索する方法は?

current_user.notebooks.find(param).notes.new(params [:item])は、ノートブックのみ、またはDBのノートレコード内のユーザーに対してのみ外部キーを作成しますか?

2番目のケース(ノートブック専用の外部キー)の場合、どのように書くべきですか?

MongoIDおよび参照関係でのMongoDBの使用

4

2 に答える 2

0

Mongoidがドキュメントの参照とクエリを管理します。必要な方向ごとに、関連付け/関係を指定してください(たとえば、User has_many:notes AND Note Belongs_to:user)。ActiveRecordのように、それは関係について「賢い」ように見えます。参照を手動で操作するのではなく、ODM(Mongoid)を機能させてください。テストを実行する(またはrailsコンソールを使用する)ときに、-f log / test.log(またはlog / development.log)を調整して、Mongoidによって実行されているMongoDB操作を確認し、実際のオブジェクトを確認できます。ドキュメントが更新されるときの参照。関係が特定のオブジェクト参照をどのように利用するかを確認できます。これに注意を払うと、リンクの最適化がより明確になるはずです。

次のモデルとテストは私のために働きます。セットアップの詳細は、リクエストに応じて入手できます。これがお役に立てば幸いです。

モデル

class User
  include Mongoid::Document
  field :name

  has_many :notebooks
  has_many :notes
end

class Note
  include Mongoid::Document
  field :text

  belongs_to :user
  belongs_to :notebook
end

class Notebook
  include Mongoid::Document

  belongs_to :user
  has_many :notes
end

テスト

require 'test_helper'

class UserTest < ActiveSupport::TestCase

  def setup
    User.delete_all
    Note.delete_all
    Notebook.delete_all
  end

  test "user" do
    user = User.create!(name: 'Charles Dickens')
    note = Note.create!(text: 'It was the best of times')
    notebook = Notebook.create!(title: 'Revolutionary France')
    user.notes << note
    assert_equal(1, user.notes.count)
    user.notebooks << notebook
    assert_equal(1, user.notebooks.count)
    notebook.notes << note
    assert_equal(1, notebook.notes.count)
    puts "user notes: " + user.notes.inspect
    puts "user notebooks: " + user.notebooks.inspect
    puts "user notebooks notes: " + user.notebooks.collect{|notebook|notebook.notes}.inspect
    puts "note user: " + note.user.inspect
    puts "note notebook: " + note.notebook.inspect
    puts "notebook user: " + notebook.user.inspect
  end

end

結果

Run options: --name=test_user

# Running tests:

user notes: [#<Note _id: 4fa430937f11ba65ce000002, _type: nil, text: "It was the best of times", user_id: BSON::ObjectId('4fa430937f11ba65ce000001'), notebook_id: BSON::ObjectId('4fa430937f11ba65ce000003')>]
user notebooks: [#<Notebook _id: 4fa430937f11ba65ce000003, _type: nil, user_id: BSON::ObjectId('4fa430937f11ba65ce000001'), title: "Revolutionary France">]
user notebooks notes: [[#<Note _id: 4fa430937f11ba65ce000002, _type: nil, text: "It was the best of times", user_id: BSON::ObjectId('4fa430937f11ba65ce000001'), notebook_id: BSON::ObjectId('4fa430937f11ba65ce000003')>]]
note user: #<User _id: 4fa430937f11ba65ce000001, _type: nil, name: "Charles Dickens">
note notebook: #<Notebook _id: 4fa430937f11ba65ce000003, _type: nil, user_id: BSON::ObjectId('4fa430937f11ba65ce000001'), title: "Revolutionary France">
notebook user: #<User _id: 4fa430937f11ba65ce000001, _type: nil, name: "Charles Dickens">
.

Finished tests in 0.018622s, 53.6999 tests/s, 161.0998 assertions/s.

1 tests, 3 assertions, 0 failures, 0 errors, 0 skips
于 2012-05-04T19:48:38.580 に答える
0

私は使うだろう

class User
  has_many :notebooks
  has_many :notes, :through => :notebooks
end

http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association

アップデート

次のように、いつでもuser_idを手動で設定できます(paramはノートブックのIDだと思いますか?):

Notebook.find(param).notes.new(params[:item].merge(:user_id => current_user.id))
于 2012-05-03T16:37:19.950 に答える