问题描述:

I have following models in my rails app:

Unit that belongs to a User

User that has many Units

I have a scope to get all the Units in the warehouse that belongs to a unit

class Unit < ActiveRecord::Base

scope :warehouse, where(:location => 'warehouse')

end

I also wrote a cached_warehouse method to get cached data, but I don't know how to read the user id from the parent model to create a cache for each User.

user = User.first

user.units.cached_warehouse

How to I access the id of the user in my cached_warehouse class method?

class Unit < ActiveRecord::Base

def self.cached_warehouse

Rails.cache.fetch([name, "warehouse_#{USER_ID}"]) do

warehouse.to_a

end

end

end

相关阅读:
Top