我有一个Scorethat belongs_to Client- 和Clienthas_one Score。
我还想在创建时分配Score给User。因此,每次 acurrent_user为特定客户创建分数时,我都希望将current_user.id其与该Score记录一起存储。
最好的方法是什么?
我在想一种优雅的方式可能是一种Score belongs_to User, :through Client但那是行不通的。
所以我假设最好的方法是添加user_id到Score模型中,然后这样做。
user_id但是,我该如何分配Score#create?
这就是我的创建操作的外观:
def create
@score = current_user.scores.new(params[:score])
respond_to do |format|
if @score.save
format.html { redirect_to @score, notice: 'Score was successfully created.' }
format.json { render json: @score, status: :created, location: @score }
else
format.html { render action: "new" }
format.json { render json: @score.errors, status: :unprocessable_entity }
end
end
结尾
这会自动将当前分数分配给哈希client_id中的params[:score]—— 但它不会对user_id.
是什么赋予了?