2012-05-03 59 views
0

假設我有一個名爲「用戶」的設計模型has_many :notes:notebooks和每個:notebook has_many :notesRails嵌套的資源和設計

所以一個音符將有兩個外鍵,:user_id:notebook_id,所以如何建立/找到一張字條?

current_user.notebooks.find(param).notes.new(params [:item])將只爲筆記本或用戶在DB中的註釋記錄中創建foreign_key?

如果第二種情況(外鍵只用於筆記本),我應該怎麼寫?

使用MongoDB的具有MongoID和引用的關係

回答

0

Mongoid將管理文檔引用和查詢你的,只要確保指定需要的每個方向關聯/關係(例如,用戶的has_many:Notes和注意belongs_to的:用戶)。像ActiveRecord一樣,它似乎對關係「很聰明」。請不要手動操作引用,而是讓你的ODM(Mongoid)爲你工作。當你運行你的測試(或使用軌道控制檯),你可以尾-f登錄/ test.log中(或日誌/ development.log),看看有什麼MongoDB的操作正在Mongoid爲你做,你可以看到實際的對象隨着文件的更新引用。你可以看到一個關係如何使用特定的對象引用,如果你注意到這一點,鏈接優化應該變得更清晰。

我下面的模型和測試工作。有關設置的詳細信息可根據要求提供。希望這有助於。

模型

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 
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,像這樣(我假設參數是你的筆記本的ID?):

Notebook.find(param).notes.new(params[:item].merge(:user_id => current_user.id)) 
+0

mongoid(ODM MongoDB的)不通過 –

+0

支持啊,對不起。沒有看到關於Mongo的那一點。請忽略。 – CambridgeMike