![](/img/trans.png)
[英]Rails3 + Devise: When to nest resource in devise_for & nested resources
[英]Rails nested resources and devise
假设我有一个名为“ User”的设计模型,它具有has_many :notes
和:notebooks
而每个:notebook
has_many :notes
。
因此,便笺将具有两个外键:user_id
和:notebook_id
,那么如何构建/查找便笺呢?
current_user.notebooks.find(param).notes.new(params [:item])将仅为笔记本或数据库中记录中的用户创建外键吗?
如果是第二种情况(仅适用于笔记本的外键),我应该怎么写?
将MongoDB与MongoID和引用关系一起使用
我会用
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))
Mongoid将为您管理文档参考和查询,只需确保为您需要的每个方向指定关联/关系即可(例如,用户has_many:notes并注意NOTE_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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.