is interesting? a stored attribute on crop or is it a method? If it's an
attribute, you can have something like:

def self.interesting
  where(:interesting => true).order('RAND()').limit(12)
end

If interesting? is a method, I'd be tempted to try to get a number of
records in one hit such that you'll be sure to find enough interesting
ones. Whether this is a good idea, depends on how much data comes back
with each record. I imagine your home page only wants to display
summary data for each crop, so perhaps this is ok:

def self.interesting
  order('RAND()').limit(100).select(&:interesting?).take(12)end

This solution avoids making an unknown number of database calls but,
if you're very unlucky and you haven't read enough records in one go,
you may fail to find the 12 records you desire.

Trade-offs :-)

Also, I'm assuming Michael's RAND solution works :-)

cheers



On 19 August 2013 11:38, Michael Pearson <[email protected]> wrote:

> No, but I can offer advice: Marshalling / demarshalling active
> ActiveRecord instances and putting them in the cache store is tricksy, and
> you may be better off fragment caching the section in the view that calls
> this code. Exactly the same mechanism (Rails.cache.fetch) can be used.
>
> Also, that doesn't look like it should be a slow operation: what might be
> the problem is the call to 'randomized' may be instantiating every single
> 'Crop' instance in your database.
>
> As a quick workaround:
>
>        interesting_crops = []
>
>        while interesting_crops.length == howmany
>          c = Crop.order('RAND()').first # or whatever your db uses for 
> randomness
>          next unless c.interesting?
>          interesting_crops.push(c)
>        end
>        interesting_crops
>
>
>
>
>
> On Mon, Aug 19, 2013 at 10:00 AM, Alex "Skud" Bayley 
> <[email protected]>wrote:
>
>>  I'm using Rails.cache to cache a slow operation on our website (fetching
>> a list of interesting crops to display on the homepage).  The code looks
>> like this:
>>
>>    def Crop.interesting
>>      return Rails.cache.fetch("interesting_crops", :expires_in => 1.day) do
>>        howmany = 12 # max number to find
>>        interesting_crops = Array.new
>>        Crop.randomized.each do |c|
>>          break if interesting_crops.length == howmany
>>          next unless c.interesting?
>>          interesting_crops.push(c)
>>        end
>>        interesting_crops
>>      end
>>    end
>>
>>
>> The result of this, the first time it's run, is a list of crops just like
>> you'd expect. However, when you run it the second time (to fetch from the
>> pre-populated cache) you get N-1 crops and the last item is a symbol called
>> :@readonly, not a crop at all, which is then causing our homepage to
>> explode with errors.
>>
>> I established this by debugging with puts, the full results of which I've
>> posted here:
>> http://lists.growstuff.org/pipermail/discuss/2013-August/002518.html
>>
>> This happens on both memory_store (on my local machine) and memcache (on
>> heroku).
>>
>> Can anyone tell what the heck is going on?
>>
>> A.
>>
>> --
>> Alex "Skud" [email protected]http://infotrope.net/
>>
>>  --
>> You received this message because you are subscribed to the Google Groups
>> "Ruby or Rails Oceania" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To post to this group, send email to [email protected].
>> Visit this group at http://groups.google.com/group/rails-oceania.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
>
>
> --
> Michael Pearson
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Ruby or Rails Oceania" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/rails-oceania.
> For more options, visit https://groups.google.com/groups/opt_out.
>



-- 


*Mark Ratjens*
*Co-founder, Habanero Software*
*
*
*
*
Sydney, Australia
[email protected]
@MarkRatjens <[email protected]>
www.habanerohq.com <http://habanerohq.com>
+61 414 159 357

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
or Rails Oceania" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/rails-oceania.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to