2011-09-11 5 views
1

트위터와 비슷한 멘션 시스템을 개발 중입니다. 그래서이 문제에서 3 가지 수업 인 Post, User 및 Mention을 다루고 있습니다. 언급 객체는 언급 된 필드 (언급 된 사용자)와 post_id (일어나는 포스트)를 가지고있다. 테스트를 작성하는 동안 이상한 문제가 발생하여 콘솔 세션을 열었습니다. 그러면 다음과 같이됩니다. 두 명의 사용자, 두 개의 게시물 및 두 개의 멘션을 만듭니다. 난 그냥 user2.id 대신 @를 사용하는 경우 @ 사용자 2, 이상하게도특정 필드에 대해 잘못된 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"> 

: 당신이 통지하는 경우 내가 두 번째 언급을 만들 때, (@ mention2는) @mentioned_id의 ID는 올바른 사용자의 ID가 아닌 그 마지막 줄, 다 잘 작동합니다 :

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

정수 (mentioned_id)를 객체 (@user)에 매핑하도록 레일스에 요청하려고합니다. @post1 = ... 행에 @other_user을 할당하면 :user이 아니라 :user_id이됩니다. 당신은 당신의 @mention1 = ... 라인에 동일한 작업을 수행해야합니다

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

그러나, 그대로 코드로,이 중 하나가 작동하지 않습니다, 당신의 attr_accessor 라인이 방해되기 때문이다.

당신도 전부 라인을 제거 할 수 있고 :mentioned => @user 부분이 작동하거나 (:mentioned보다는 :mentioned_id에 직접 할당 할 수 있도록, 그래서 같이 변경할 수 있습니다 그래, 그들은 정말 같은 일이지만, 그것은 보인다 레일스는 attr_accessible을 확인하고 :mentioned이 실제로는 단지 :mentioned_id임을 알기 전에 :mentioned 업데이트에 대한 액세스를 거부합니다.

+0

이전에 시도해 보니 제대로 작동하지 않았습니다. 그럴 경우 언급의 언급 된 필드는 아무 이유없이 nil로 설정됩니다. –

+0

ruby-1.9.2-p180 : 005> p.mentions.create : mention => user1 => #

+0

'p.mentions.create : mention => user1' 항목을 콘솔에 넣으면 "경고 : 속성 : 언급했다 ". 이는'Mention' 모델의'attr_accessible : mention_id' 행 때문입니다. 그것을 없애면 효과가 있습니다. –

관련 문제