0
Padrino 앱이 있습니다. counter_cache를 사용할 위치를 만들고 있습니다. 내 ORM ActiveRecord 사용하고 있습니다. 저의 저장소 모델에서는 주어진 저장소와 관련된 기여도의 수를 유지하려고합니다. 다음은 관련 모델은 다음과 같이Padrino (Rails)에서 Counter_Cache를 올바르게 업데이트 할 수 없습니다.
class Repository < ActiveRecord::Base
has_many :contributions, autosave: true
has_many :users, through: :contributions
validates :full_name, presence: true, uniqueness: true
end
class Contribution < ActiveRecord::Base
belongs_to :user
belongs_to :repository, counter_cache: true
end
class User < ActiveRecord::Base
has_many :contributions
has_many :repositories, through: :contributions
validates :username, presence: true, uniqueness: true
end
스키마는 다음과 같습니다
나는 contributions_count가 제대로 업데이트되었는지 확인하기 위해 RSpec에의 테스트를 만들었습니다create_table "contributions", :force => true do |t|
t.integer "user_id"
t.integer "repository_id"
end
create_table "repositories", :force => true do |t|
t.string "full_name"
t.integer "contributions_count", :default => 0
end
create_table "users", :force => true do |t|
t.string "username"
end
. 그러나, 나는 그것을 통과시킬 수 없다. 여기 사양입니다 :
describe "when a new contribution is created" do
it "updates the counter cache" do
repo = Repository.create(full_name: "sample_repo")
user = User.create(username: "sample_user")
expect {
Contribution.create(user: user, repository: repo)
}.to change {repo.contributions_count }.by(1)
end
end
내가 사양을 실행하면, 나는 다음과 같은 오류가 얻을 :
1) Repository when a new contribution is created updates the counter cache
Failure/Error: expect {
result should have been changed by 1, but was changed by 0
# ./spec/models/repository_spec.rb:43:in `block (3 levels) in <top (required)>'
나는 또한 콘솔에 기여를 만드는 시도하고 저장소 counter_cache를 업데이트되지 않습니다. 나는 물건을 잔뜩 시도했지만 제대로 작동하도록하는 방법을 알아낼 수 없습니다. 어떤 도움을 주시면 감사하겠습니다. 감사.
제안 내가 코드를 업데이트하지만 지금은 다음과 같은 오류 '장애/오류를 얻을 : repo.reload.contributions_count 액티브 :: RecordNotFound : 수 없음 ID없는 저장소 찾기' –