I cannot figure out why the current_user is always set to user_id = 1.
When it edits the data, it sets user_id appropriately but when it is creating a new record, it sets user_id =1
Using rails 3.1.3 w/ Devise
The controller:
def update_multiple
@user = current_user
for @workoutdate in params[:workout_workoutdates]
is_new = false
#this part works, user_id ends up equaling the current_user
@workouts = Workout.where(:user_id => @user,
:workoutdate => @workoutdate.to_time)
if !@workouts.any?
# when setting user_id to an integer it works
# for example, workout = Workout.new(:user_id => 3), it works
workout = Workout.new(:user_id => @user,
:workoutdate => @workoutdate.to_time)
is_new = true
else
workout = @workouts[0]
end
workout.save
end
And the resulting SQL from the terminal:
SQL (0.7ms) INSERT INTO "intervals" ("created_at", "interval_name", "updated_at", "workout_id") VALUES (?, ?, ?, ?) [["created_at", Tue, 13 Dec 2011 03:24:28 UTC +00:00], ["interval_name", "INTERVAL"], ["updated_at", Tue, 13 Dec 2011 03:24:28 UTC +00:00], ["workout_id", nil]]
SQL (0.5ms) INSERT INTO "workouts" ("created_at", "updated_at", "user_id", "workoutdate") VALUES (?, ?, ?, ?) [["created_at", Tue, 13 Dec 2011 03:24:28 UTC +00:00], ["updated_at", Tue, 13 Dec 2011 03:24:28 UTC +00:00], ["user_id", 1], ["workoutdate", Thu, 08 Dec 2011 08:00:00 UTC +00:00]]
I use devise differently. I think you’re making it too difficult. With devise, you can reference a users objects (workouts) like this:
and then create a new one like so:
and so forth…