簡體   English   中英

Rails為特定字段創建具有錯誤ID的對象

[英]Rails creating object with the wrong id for certain field

我正在開發類似於Twitter中的提及系統。 因此,我正在處理此問題中的三個類:Post,User和Mention。 提到對象有一個提到的ID字段(提到的用戶)和一個post_id(發生它的帖子)。 我在編寫測試時發現了一些奇怪的問題,所以我打開了一個控制台會話,結果就是這樣:我創建了兩個用戶,兩個帖子和兩個提及。 如果您注意到,當我創建第二個提及(@ mention2)時,@提及的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"> 

奇怪的是,如果我使用@ user2.id而不是最后一行僅使用@ user2,則一切正常:

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


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

誰能告訴我發生了什么事? 我以為rails“ magic”會在引用對象時負責獲取id。 為何與第一個用戶一起使用而不與第二個用戶一起使用?

這是我的提及類的代碼:

class Mention < ActiveRecord::Base
  attr_accessible :mentioned_id

  belongs_to :mentioned, :class_name => "User"
  belongs_to :post

  validates :mentioned, :presence => true
  validates :post, :presence => true

end

這是我的用戶類的代碼的相關部分:

    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

最后一行僅在使用.id方法的情況下才能正常運行,但前兩行可以正常運行...

您試圖讓Rails將一個整數( mentioned_id @user )映射到一個對象( @user )。 您在@post1 = ...行上正確地執行了此操作,在這里將@other_user分配給:user ,而不是:user_id 您需要在@mention1 = ...行上執行相同的操作:

@mention1 = @post1.mentions.create :mentioned => @user

但是,就您的代碼而言,這也不起作用,因為您的attr_accessor行會產生干擾。

您可以完全刪除該行,然后:mentioned => @user部分將起作用,或者您可以像這樣進行更改,以允許直接分配給:mentioned而不是:mentioned_id :mentioned => @user (是的,它們確實是同一回事,但似乎Rails會在意識到:mentioned確實只是:mentioned_id attr_accessible之前,先檢查attr_accessible並拒絕訪問更新的:mentioned

attr_accessible :mentioned_id, :mentioned

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM