I have a form where user can submit a review to a product without having to log in first. When user clicks the submit button, ReviewsController
calls auth_user
method which creates a new Review
object with the review content updated. But, I want to associate this Review
object with the user who is going to sign up. Right now, I'm simply retrieving the last Review
object like this, Review.last
. However, I know that this is vulnerable to multiple users creating reviews and signing up at the same time. Suppose that User A submits a review, User B submits a review, and User A signs up. In this scenario, it will retrieve the Review
object created by the User B instead of the one created by the User A and associate the record with the User A. But, I want the one created by the User A and associate it with the User A regardless of how many users are creating the records simultaneously. Does anybody have an idea of how to go about doing this?
Asked
Active
Viewed 43 times
0

kchoi
- 1,205
- 5
- 18
- 32
1 Answers
1
You could store the IP of the request when storing the reviews and then when the user logs in, match the IP's and associate the user. You can get the IP as below
remote_ip = request.env["HTTP_X_FORWARDED_FOR"]
Also you could use user agents and cookies apart from IP address. Also check this question