私は属している - と has_one を持っScoreてClientいClientますScore。
また、作成時Scoreにを割り当てたいと思います。Userそのため、 が特定のクライアントのスコアを作成するたびに、がそのレコードとともに保存されるようにしcurrent_userたいと考えています。current_user.idScore
それを行う最善の方法は何ですか?
エレガントな方法があるかもしれないと思っていました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。
何を与える?