트위터와 비슷한 멘션 시스템을 개발 중입니다. 그래서이 문제에서 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 메서드를 사용하면 올바르게 작동하지만 이전 두 메서드는 올바르게 작동합니다. ..
이전에 시도해 보니 제대로 작동하지 않았습니다. 그럴 경우 언급의 언급 된 필드는 아무 이유없이 nil로 설정됩니다. –
ruby-1.9.2-p180 : 005> p.mentions.create : mention => user1 => # –
'p.mentions.create : mention => user1' 항목을 콘솔에 넣으면 "경고 : 속성 : 언급했다 ". 이는'Mention' 모델의'attr_accessible : mention_id' 행 때문입니다. 그것을 없애면 효과가 있습니다. –