In my controller, I need to pluck a single, matching integer value and then create a record with that value in another table. In action, it looks like this:
if Participation.where(ranking: "1")
first = PointsAllocation.where(game_id: params[:game_id]).where(place: "1").pluck(:points)
Stack.create(game_id: params[:game_id], user_id: current_user.id, chips: first)
else
end
I have tested in the console that the first variable is definable. If I run PointsAllocation.where(game_id: "1").where(place: "1").pluck(:points), it will return:
SELECT "points_allocations"."points" FROM "points_allocations" WHERE "points_allocations"."game_id" = 1 AND "points_allocations"."place" = 1
=> [10]
Ok, so it is correctly plucking what looks like an integer value for Points. I then want to use this points value and send it to the Chips column in the Stack table. When I run this, it will add a nil record even though first is defined, like so:
<Stack id: 1, game_id: 1, user_id: 1, chips: nil>
In troubleshooting, I thought maybe the issue here is that even though it looks like an integer (and Chips, I should add, is a t.integer attribute), maybe it's accidentally a string or something from pluck. So let's map this to an integer to be safe by adding map(&:to_i) after the pluck.
When I do that, it gets weirder, as it now returns:
<Stack id: 9, game_id: 1, user_id: 1, chips: 0>
So when I convert it to an integer, it changes 10 to a 0 and adds it to the table.
Where am I going wrong?