세 가지 모델이 있습니다 : 작업, 사용자 및 응답.모델 내에서 다른 레코드 만들기 : 나쁜 습관?
사용자가 작업을 완료하면 결과가 응답으로 저장됩니다. 이 응답 시간 동안 사용자는 대변 점수를받습니다.
내 첫 번째 질문은, 포인트 속성을 업데이트하기위한 논리가 어디로 가야합니까? 작업, 사용자 또는 응답 모델 내부? 현재 Response 클래스에서 Response.task.points를 잡고 User.task.points에 해당 값을 추가합니다.
# POST /responses
# POST /responses.json
def create
@response = Response.new(response_params)
respond_to do |format|
if @response.save
@response.reward_user
format.html { redirect_to @response, notice: 'Response was successfully created.' }
format.json { render :show, status: :created, location: @response }
else
format.html { render :new }
format.json { render json: @response.errors, status: :unprocessable_entity }
end
end
end
두 번째 것은 내가 모든 점 트랜잭션을 기록 할 것입니다 : 같은
Response.create는 보인다. 그래서 저는 points_transaction이라는 또 다른 모델을 만들었습니다. 내 다른 질문은, 어디서 points_transaction을 작성해야합니까? Response.create 컨트롤러에서? 응답 모델에서?
Response create 메소드 내에서 PointsTransaction을 만드는 것이 잘못되었지만 모델 내에서 PointsTransaction을 만드는 것도 잘못되었습니다. MVC가 정확한 것은 어느 것입니까?
내 Response 개체는 다음과 같습니다 : 당신이 간단한 MVC 넘어 갈 필요가있을 때
class Response < ApplicationRecord
belongs_to :task, optional: true
belongs_to :user, optional: true
def reward_user
point_value = task.point_value
user.points += point_value
PointTransaction.new({/*params go here*/})
end
end
좋은 질문이 있습니다. 나는 당신이 당신의 아키텍처 솔루션의 다음 단계로 이동할 필요가있는 시점에 도달했다고 생각합니다. – AntonTkachov