일부 레일스 2.3.5 ActiveRecord 동작으로 실행되고 있습니다. 이해가되지 않습니다. 객체가 일관성없는 방식으로 업데이트 된 연관 ID를 가질 수 있습니다. 레일스 ActiveRecord 연관성이 일관성없이 업데이트되었습니다.
이
가장 예를 들어 설명한다 :문자열과 Post
모델을 작성 'title'
속성과 문자열을 가진 Comment
모델은 'content'
때문이다.
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
end
시나리오 # 1 : 여기
는 연관있는 다음 코드에서 나는 관련Comment
하나
Post
는, 첫 번째를 보내고 '
find
에 의해 두 번째
Post
을 만들에 두 번째
Comment
를 추가 작성 먼저
Post
을 찾아 두 번째
Post
에 명시적인 할당없이 두 번째
Comment
이 연결되어 있음을 확인하십시오.
post1 = Post.new
post1 = Post.new(:title => 'Post 1')
comment1 = Comment.new(:content => 'content 1')
post1.comments << comment1
post1.save
# Create a second Post object by find'ing the first
post2 = Post.find_by_title('Post 1')
# Add a new Comment to the first Post object
comment2 = Comment.new(:content => 'content 2')
post1.comments << comment2
# Note that both Comments are associated with both Post objects even
# though I never explicitly associated it with post2.
post1.comment_ids # => [12, 13]
post2.comment_ids # => [12, 13]
시나리오 # 2 : 다시 위의 명령을 실행하지만, 이번에는 그것의 얼굴에, 결과에 영향을 미치지 않습니다, 하나 개의 추가 명령을 삽입합니다. 추가 명령은 이후에이 발생하고 comment2
및 이 전에 post1
이 추가되기 전에 발생하는 post2.comments
입니다
post2
과 관련된 유일한
하나 의견이 있음을
post1 = Post.new
post1 = Post.new(:title => 'Post 1A')
comment1 = Comment.new(:content => 'content 1A')
post1.comments << comment1
post1.save
# Create a second Post object by find'ing the first
post2 = Post.find_by_title('Post 1A')
# Add a new Comment to the first Post object
comment2 = Comment.new(:content => 'content 2A')
post2.comments # !! THIS IS THE EXTRA COMMAND !!
post1.comments << comment2
# Note that both Comments are associated with both Post objects even
# though I never explicitly associated it with post2.
post1.comment_ids # => [14, 15]
post2.comment_ids # => [14]
참고.
큰 질문 : Comment
을 post1
에 새로 추가하기 전에 post2.comments
을 실행하면 왜 의견이 post2
과 관련이 있습니까?
답변 해 주셔서 감사합니다. 그러나이 행동이 잘못된 것처럼 보이지 않습니까? 캐싱은 결과의 불일치를 초래하지 않고 성능을 향상시켜야합니다. – rlandster
더 많은 동시성 문제가 있습니다. Rails는 외부 소스가 모델 인스턴스와 관련된 사항을 변경하지 않을 것으로 기대합니다.이 경우 외부 소스는 특정 인스턴스 이외의 곳에서 발생하는 모든 동작을 의미합니다. 이것이 틀렸다는 것을 강력히 믿는다면, 버그 리포트를 제출하십시오. – EmFi