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

  • Home
  • SEARCH
  • 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 7968289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:03:11+00:00 2026-06-04T07:03:11+00:00

I have a controller action that calls a model method which generates a serialized

  • 0

I have a controller action that calls a model method which generates a serialized list of data pulled from another model database. I need this to be uncached because the SQL queries should be random data pulls.

Here’s a general idea of my code (Note that User has_one Foo, Bar is an arbitrary model of data, :data_list is of type text, and the database is SQLite):

# app/models/foo.rb

class Foo < ActiveRecord::Base
  serialize :data_list

  def generate_data
    list = []
    for i in 1..4
      data = Bar.find(:first, :order => "Random()")
      list << data
    end
    self.data_list = list
  end
end

# app/controllers/users_controller.rb

class UsersController < ApplicationController
  def generate_action
    ...
    uncached do
      @user.foo.generate_data
    end
    @user.foo.save
  end
end

# app/views/user/show.html.erb

...
<% @user.foo.data_list.each do |data| %>
  <%= data %><br />
<% end %>

Whenever uncached do ... end is removed, everything works fine and the show view prints out each set of Bar objects in @user.foo.data_list. Unfortunately, because of Rails’ SQL caching, it ends up look like this:

RandomDataPoint8
RandomDataPoint8
RandomDataPoint8
RandomDataPoint8

When I need to look like this:

RandomDataPoint7
RandomDataPoint13
RandomDataPoint2
RandomDataPoint21

It should be noted that running user.foo.generate_data from Rails command line works perfectly with the randomization. It is only when being called from the controller that caching starts to occur.

My research suggested I use uncached in the controller to remove caching, however it seems to destroy my data serialization and I receive the error:

undefined method 'each' for #<String:0x007ff49008dc70>

In fact, it does this even if I retroactively add in uncached (having successfully generated a data_plan without uncached prior) and save the controller, but don’t call generate_action.

  • 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-04T07:03:13+00:00Added an answer on June 4, 2026 at 7:03 am

    EDIT
    I believe this problem is actually related to the fact that I was storing an object in the hash. Switching to the object id fixed this problem. Another SO question of mine regarding this can be found here:
    Rails – Accessing serialized data from console

    The following has been preserved just because the syntax may still help people, but I don’t believe it was the actual cause of the problem.


    I solved this by moving uncached to the model. For reference, the source I was using to originally solve this problem was this link: http://railspikes.com/2008/8/18/disabling-activerecord-query-caching-when-needed

    What I overlooked is that he puts uncached in the model, not the controller. Also, the syntax needed to be a little different:

    # app/models/foo.rb
    
    self.class.uncached do
       ...
    end
    

    instead of

    uncached do
      ...
    end
    

    The source for the syntax correction is this SO response: https://stackoverflow.com/a/967690/337903

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Rails controller action to test. In that action, a method User.can?
I have a spring action that I am rendering some json from the controller,
I have a controller POST action that redirects to a GET method using RedirectToAction.
I have an Ajax.ActionLink that successfully calls my controller action but instead of updating
I have a controller action that checks this.User.Identity.IsAuthenticated What do you suggest how to
I have a controller action that builds a pdf and than downloads it by
If i have a Controller Action that may recieve both HTTP GET and HTTP
If i have a controller action Create that returns a view with the following
I have a controller's action and view page that uses a master page. The
I'd like to have URLs that are even shorter than /{Controller}/{Action}/{Id}. For example, I'd

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.