Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8915067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:53:09+00:00 2026-06-15T04:53:09+00:00

Using Rails 3.2. I have the following code: # photo.rb class Photo < ActiveRecord::Base

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T04:53:10+00:00Added an answer on June 15, 2026 at 4:53 am

    you can do memoization, something as follow

    class Photo < ActiveRecord::Base
      extend ActiveSupport::Memoizable
      after_create :associate_current_user
    
      private
    
      def associate_current_user
        current_user = UserSession.find.user
        self.user_id = current_user.id
      end
      associate_current_user
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Rails 3.2. I have the following code: # photo.rb class Photo < ActiveRecord::Base
Using Ruby 1.9.2, Rails 3.1. I have the following code: # review.rb def calculate_rating(reviewable)
I'm using jQuery with rails and have the following piece of code $('#related').html(<%= render
I am using Rails 3.2. I have following models: Blog Comment User class Blog
Using Rails 3.2.0.rc2 and ruby 1.9.3p0 In app/views/requests/_form.html.erb I have the following code for
In my rails app, i am using a legacy database. class Expression < ActiveRecord::Base
In a Rails application using prototype, I have the following (simplified) code in my
I am using Rails 3.0.9 with the ckedit gem. I have the following code
I am using Ruby on Rails v3.0.9 and I have the following code in
I am using Rails 3.2.x and I have following code in view. <%= order.address.address_array.join('<br

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.