Using Rails 3.2. I have the following code:
# photo.rb
class Photo < ActiveRecord::Base
after_create :associate_current_user
private
def associate_current_user
current_user = UserSession.find.user
self.user_id = current_user.id
end
end
Before a new record is created, it searches for the current_user. This is alright if it’s just 1 new record at a time. But if there are 100 records to be created, it’s gonna search for the same current_user 100 times. There is definitely performance issue.
Is there a way I can cache the object for similar operation, or any suggestion?
Thanks.
you can do memoization, something as follow