未添加嵌入文档

发布于 2024-10-18 05:57:39 字数 1722 浏览 2 评论 0原文

添加嵌入文档时遇到问题。我正在尝试添加一个嵌入用户中的标签。

user.rb

class User
  include Mongoid::Document
  field :name

  validates_presence_of :name
  validates_uniqueness_of :name, :email, :case_sensitive => false      
  attr_accessible :name, :email, :password, :password_confirmation

  embeds_many :tags
  embeds_many :tasks

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

tag.rb

class Tag
  include Mongoid::Document
  field :name
  embedded_in :user, :inverse_of => :tags
  references_many :tasks
end

Tags_controller.rb

  def create
    #@user = User.find(:first, :conditions => {:_id => "4d3ae09bf5c4930b2b000004"} )
    @user = current_user
    @tag = Tag.new(params[:tag])

    @user.tags << @tag
    @tag.save

    redirect_to @tag, :notice => "Tag created!" 
  end

这是我尝试创建新标签时到服务器的输出。

Started POST "/tags" for 127.0.0.1 at 2011-02-18 13:46:03 -0500   
Processing by TagsController#create as HTML   Parameters: {"utf8"=>"✓", "authenticity_token"=>"6p+Jova7Hol2v5LRReSp2fhNJ967EwkeIzAWyrChQRE=", "tag"=>{"name"=>"general"}, "commit"=>"Create Tag"} 
db['users'].find({:_id=>BSON::ObjectId('4d39cd63f5c4930708000001')}, {}).limit(-1) MONGODB 
db['users'].update({"_id"=>BSON::ObjectId('4d39cd63f5c4930708000001')}, {"$push"=>{"tags"=>{"name"=>"general", "_id"=>BSON::ObjectId('4d5ebe6bf5c493554d000002')}}}) Redirected to 
http://localhost:3000/tags/4d5ebe6bf5c493554d000002 Completed 302 Found in 5ms

不太确定问题是什么或从哪里开始。实际上看起来好像找到了用户,然后正在对标签进行更新,但没有成功。

谢谢

Having trouble adding an embedded document. I am trying to add a tag which is embedded in the user.

user.rb

class User
  include Mongoid::Document
  field :name

  validates_presence_of :name
  validates_uniqueness_of :name, :email, :case_sensitive => false      
  attr_accessible :name, :email, :password, :password_confirmation

  embeds_many :tags
  embeds_many :tasks

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

tag.rb

class Tag
  include Mongoid::Document
  field :name
  embedded_in :user, :inverse_of => :tags
  references_many :tasks
end

tags_controller.rb

  def create
    #@user = User.find(:first, :conditions => {:_id => "4d3ae09bf5c4930b2b000004"} )
    @user = current_user
    @tag = Tag.new(params[:tag])

    @user.tags << @tag
    @tag.save

    redirect_to @tag, :notice => "Tag created!" 
  end

This is the output to the server when I try to create a new tag.

Started POST "/tags" for 127.0.0.1 at 2011-02-18 13:46:03 -0500   
Processing by TagsController#create as HTML   Parameters: {"utf8"=>"✓", "authenticity_token"=>"6p+Jova7Hol2v5LRReSp2fhNJ967EwkeIzAWyrChQRE=", "tag"=>{"name"=>"general"}, "commit"=>"Create Tag"} 
db['users'].find({:_id=>BSON::ObjectId('4d39cd63f5c4930708000001')}, {}).limit(-1) MONGODB 
db['users'].update({"_id"=>BSON::ObjectId('4d39cd63f5c4930708000001')}, {"$push"=>{"tags"=>{"name"=>"general", "_id"=>BSON::ObjectId('4d5ebe6bf5c493554d000002')}}}) Redirected to 
http://localhost:3000/tags/4d5ebe6bf5c493554d000002 Completed 302 Found in 5ms

Not really sure what the issue is or where to start. It actually looks like the user is found then an update is being made to tags but it is not successful.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅哥哥的热头脑 2024-10-25 05:57:39

模型中的 Tags 类嵌入到用户内部(通过 embeds_many 关联),而不是单独的表。因此,在控制器中进行更新后,您应该会看到如下内容:

> db.users.find() 
{ 
    _id: ObjectId('4d39cd63f5c4930708000001'),
    tags: [
        {
            _id: ObjectId('4d5ebe6bf5c493554d000002'),
            name: "General"
        }
    ]
}

使用 MongoID,您还可以通过将“embeds_many”替换为“references_many”,让标签出现在自己的集合中。

在上面的评论中,您会看到 berek-bryan 的问题与添加标签的位置有关。他希望该标签能够添加到自己的集合中,因此提出了这个问题。实际上,标签被直接添加到他的用户集合中。

The Tags class in your model is embedded inside of user (via the embeds_many association), rather than a table on its own. So following the updates in your controller, you should have something like this:

> db.users.find() 
{ 
    _id: ObjectId('4d39cd63f5c4930708000001'),
    tags: [
        {
            _id: ObjectId('4d5ebe6bf5c493554d000002'),
            name: "General"
        }
    ]
}

Using MongoID, you can also have Tags appear in their own collection by replacing "embeds_many" with "references_many".

In the comments above, you'll see that the issue berek-bryan was having had to do with where the tag was being added. He expected the tag to be added in its own collection, hence the question. Actually, the tags were being added right into his users collection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文