Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails为特定字段创建id错误的对象_Ruby On Rails_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails Rails为特定字段创建id错误的对象

Ruby on rails Rails为特定字段创建id错误的对象,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我正在开发一个类似于twitter中的提及系统。因此,在这个问题中,我要处理三个类:Post、User和notice。提到对象有一个提到的id字段(提到的用户)和一个帖子id(发生的帖子)。 我在编写测试时发现了一些奇怪的问题,所以我打开了一个控制台会话,结果如下: 我创建了两个用户、两篇帖子和两次提及。如果您注意到,当我创建第二个提及(@sinet2)时,@sineted_id的id不是正确的用户id: ruby-1.9.2-p180 :001 > @user1 = Factory(:u

我正在开发一个类似于twitter中的提及系统。因此,在这个问题中,我要处理三个类:Post、User和notice。提到对象有一个提到的id字段(提到的用户)和一个帖子id(发生的帖子)。 我在编写测试时发现了一些奇怪的问题,所以我打开了一个控制台会话,结果如下: 我创建了两个用户、两篇帖子和两次提及。如果您注意到,当我创建第二个提及(@sinet2)时,@sineted_id的id不是正确的用户id:

ruby-1.9.2-p180 :001 > @user1 = Factory(:user, :email => Factory.next(:email))

 => #<User id: 1, name: "Fulano", email: "person_number1@example.com", created_at: "2011-09-11 15:38:11", updated_at: "2011-09-11 15:38:11", encrypted_password: "ede4e8cc0440b8bd9fdc059e8496154715b6592690157aac618...", salt: "4bfae8fecee1629b7c290c2d5af5bd3bbeb38c4ce76546d7176...", admin: false> 

ruby-1.9.2-p180 :002 > @user2 = Factory(:user, :email => Factory.next(:email))

 => #<User id: 2, name: "Fulano", email: "person_number2@example.com", created_at: "2011-09-11 15:39:06", updated_at: "2011-09-11 15:39:06", encrypted_password: "ddda10f48575f63724e1db3d3cf684f2c60380325236311e15d...", salt: "911121b8942dc57116dfd24383d96874c750bc5a21fa210288b...", admin: false> 

ruby-1.9.2-p180 :003 > @post1 = Factory(:post, :user => @user1)

 => #<post id: 1, content: "Foo bar", user_id: 1, created_at: "2011-09-11 15:39:57", updated_at: "2011-09-11 15:39:57"> 

ruby-1.9.2-p180 :004 > @post2 = Factory(:post, :user => @user2)

 => #<post id: 2, content: "Foo bar", user_id: 2, created_at: "2011-09-11 15:40:37", updated_at: "2011-09-11 15:40:37"> 

ruby-1.9.2-p180 :005 > @mention1 = @post2.mentions.create :mentioned_id => @user1

 => #<Mention id: 1, post_id: 2, mentioned_id: 1, created_at: "2011-09-11 15:41:33", updated_at: "2011-09-11 15:41:33"> 

ruby-1.9.2-p180 :007 > @mention2 = @post1.mentions.create :mentioned_id => @user2

 => #<Mention id: 2, post_id: 1, mentioned_id: 1, created_at: "2011-09-11 15:42:16", updated_at: "2011-09-11 15:42:16"> 

只有在使用.id方法时,最后一行才能正常工作,但前两行工作正常…

您试图要求Rails将整数(
提到的\u id
)映射到对象(
@user
)。您在
@post1=…
行中正确地执行了此操作,您将
@other\u user
分配给
:user
,而不是
:user\u id
。您需要在您的
@attention1=…
行上执行相同的操作:

@mention1 = @post1.mentions.create :mentioned => @user
然而,对于代码本身,这也不会起作用,因为您的
attr\u访问器
行正在干扰

您可以完全删除该行,
:refered=>@user
部分将起作用,也可以这样更改它,以允许直接分配给
:refered
,而不是
:refered\u id
(是的,它们实际上是一样的,但Rails似乎在意识到
:refered
实际上只是
:refered
之前检查
attr\u accessible
,并拒绝更新
:refered


您正试图要求Rails将一个整数(
提到的用户id
)映射到一个对象(
@user
)。您在
@post1=…
行上正确地进行了映射,您正在将
@other\u user
分配给
:user
,而不是
:user\u id
。您需要在
@antegion1=…
行上执行同样的操作:

@mention1 = @post1.mentions.create :mentioned => @user
然而,对于代码本身,这也不会起作用,因为您的
attr\u访问器
行正在干扰

您可以完全删除该行,
:refered=>@user
部分将起作用,也可以这样更改它,以允许直接分配给
:refered
,而不是
:refered\u id
(是的,它们实际上是一样的,但Rails似乎在意识到
:refered
实际上只是
:refered
之前检查
attr\u accessible
,并拒绝更新
:refered


我以前试过,但它似乎不起作用。如果我这样做,提及的id字段设置为nil,出于某种原因ruby-1.9.2-p180:005>p.indications.create:indicated=>user1=>#当您在控制台中执行
p.indications.create:indicated=>user1
条目时,您可能会看到“警告:无法批量分配受保护的属性:已提及。”"。这是因为你的
attr\u accessible:attr\u accessible在你的
att
模型中提到了id
行。去掉它,它就会起作用。这很有效,你应该编辑你的回复并添加它,这样我就可以将它标记为已接受。谢谢你。我想我还没有完全理解attr\u accessible的作用,但我读到有必要定义attr\u accessible出于安全原因,请不要删除它。是否通过批量分配使每个属性都可编辑?我在前面编辑了回复,请参见最后一行。如果您有一个模型,不希望用户修改某些字段,那么您需要使用
attr\u accessible
,以确保他们只能修改您想要的字段。我将进行编辑我的回答再次向您展示了这一点。我以前尝试过,但它似乎不起作用。如果我这样做,提及的id字段将设置为零,出于某种原因Ruby-1.9.2-p180:005>p.intriends.create:intriends=>user1=>#当您在控制台中执行
p.intriends.create:intriends=>user1
条目时,您可能是“警告:无法批量分配受保护的属性:已提及”。这是因为你的
attr\u accessible:attr\u accessible在你的
att
模型中提到了id
行。去掉它,它就会起作用。这很有效,你应该编辑你的回复并添加它,这样我就可以将它标记为已接受。谢谢你。我想我还没有完全理解attr\u accessible的作用,但我读到有必要定义attr\u accessible出于安全原因,请不要删除它。是否通过批量分配使每个属性都可编辑?我在前面编辑了回复,请参见最后一行。如果您有一个模型,不希望用户修改某些字段,那么您需要使用
attr\u accessible
,以确保他们只能修改您想要的字段。我将进行编辑我再次给出我的答案,以表明这对你是如何起作用的。
    Class User < ActiveRecord::Base

  …

  has_many :posts, :dependent => :destroy  
  has_many :mentions, :dependent => :destroy, :foreign_key => 'mentioned_id'

  …
 @user = Factory(:user, :email => Factory.next(:email))
  @other_user = Factory(:user, :email => Factory.next(:email))

  @post1 = Factory(:post, :user => @other_user)
  @post2 = Factory(:post, :user => @other_user)
  @post3 = Factory(:post, :user => @user) 
  @mention1 = @post1.mentions.create :mentioned_id => @user
  @mention2 = @post2.mentions.create :mentioned_id => @user 
  @mention3 = @post3.mentions.create :mentioned_id => @other_user.id
@mention1 = @post1.mentions.create :mentioned => @user
attr_accessible :mentioned_id, :mentioned