私は、タスク、ユーザー、および応答の3つのモデルを持っています。モデル内から別のレコードを作成する:悪い練習ですか?
ユーザーがタスクを完了すると、結果が応答として保存されます。その応答時間の間に、ユーザはクレジットポイントを獲得する。
私の最初の質問は、ポイント属性を更新するロジックはどこに行くのですか?タスク、ユーザー、または応答モデルの内部?現在のところ、Response.task.pointsを取得してその値をUser.task.pointsに追加するResponseモデルにあります。
ようResponse.createが、その後になります。二つ目は、私はすべてのポイントの取引を記録したいということです
# 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
。ですから、私はpoints_transactionという別のモデルを作成しました。私の他の質問は、どこにpoints_transactionを作成すればよいのでしょうか? Response.createコントローラでは?応答モデルでは?
レスポンスの作成メソッドからPointsTransactionを作成するのは間違っているようですが、モデル内から作成することも同様に間違っているようです。どちらがMVC正しいのですか?
マイResponseオブジェクトは、次のようになります。
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