2011-09-11 29 views
1

我正在開發類似於twitter中的提及系統。所以我在處理這個問題中的三個類:Post,User和Mention。提及對象具有一個mentioned_id字段(提到的用戶)和一個post_id(它發生的帖子)。 我在編寫測試時發現了一些奇怪的問題,所以我打開了一個控制檯會話,這是發生了什麼事情: 我創建了兩個用戶,兩個帖子和兩個提及。如果您發現,當我創建了第二個提及(@ mention2)@mentioned_id的ID不正確的用戶ID:Rails使用特定字段的錯誤ID創建對象

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

=> #<User id: 1, name: "Fulano", email: "[email protected]", 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: "[email protected]", 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"> 

任何人都可以請告訴我發生了什麼?我認爲導軌「魔術」在引用對象時負責獲取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方法,但前兩項工作正確,最後一行只能正常工作。 ..

回答

1

您試圖要求Rails將整數(mentioned_id)映射到對象(@user)。您正在@post1 = ...行上正確執行此操作,您將@other_user指定爲:user,而不是:user_id。你需要做同樣的你的@mention1 = ...線:

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

然而,隨着你的代碼,因爲它是,這將不能工作,因爲你的attr_accessor線的干擾。

您可以擺脫完全行和:mentioned => @user部分將工作,或者你可以改變它像這樣,允許直接分配給:mentioned而非:mentioned_id(是的,他們真的是同樣的事情,但似乎Rails檢查attr_accessible並拒絕訪問更新:mentioned,然後才意識到:mentioned實際上只是:mentioned_id)。

attr_accessible :mentioned_id, :mentioned 
+0

我以前試過,它似乎沒有工作。如果我這樣做,提的mentioned_id字段設置爲無,因爲某些原因 –

+0

紅寶石1.9.2-P180:005> p.mentions.create:提到=> USER1 =>#<提及ID:無,POST_ID: 1,mentioned_id:nil,created_at:nil,updated_at:nil> –

+0

當您在控制檯中執行'p.mentions.create:mentioned => user1'項時,您可能會看到「WARNING:Can not mass-assign protected屬性:提到「。這是因爲你的'Mention'模型中有'attr_accessible:mentioned_id'這行。擺脫它,它會工作。 –