开发者

Existing Rails model without fetching it from the database

Does anyone know if its possible to create a model instance and apply the ID and any other attributes without having to load it from the database? I tried doing this, but the associations are not fetched from the database :( Any ideas?


EDI开发者_StackOverflow社区T

What I want to accomplish is simply this:

  1. Fetch an existing record from the database.
  2. Store as "hashed" output of the record into redis or some other memory store.
  3. Next time when that record is fetched, fetch the cached store first and if it is not found then goto step 1.
  4. If there is a cache hit, then load all the cached attributes into that model and make that model instance behave as if it were a model fetched from the database with a finite set of columns.

This is where I am stuck, what I've been doing is creating a Model.new object and setting each of the params manually. This works, but it treats the instantiated model object as a new record. There has got to be an intermediate subroutine in ActiveRecord that does the attribute setting.


I solved the problem by doing the following.

  1. Create a new model class which extends the model class that I want to have cached into memory.

  2. Set the table_name of the new class to the same one as the parent class.

  3. Create a new initialize method, call the super method in it, and then allow a parameter of that method to allow for a hash variable containing all the properties of the parent class.

  4. Overload the method new_record? and set that to false so that the associations work.

Here's my code:

class Session < User

  self.table_name = 'users'

  METHODS = [:id, :username] # all the columns that you wish to have in the memory hash
  METHODS.each do |method|
    attr_accessor method
  end

  def initialize(data)
    super({})

    if data.is_a?(User)
      user = data
      data = {}
      METHODS.each do |key|
        data[key] = user.send(key)
      end
    else
      data = JSON.parse(data)
    end

    data.each do |key,value|
      key = key.to_s
      self.send(key+'=',value)
    end
  end

  def new_record?
    false
  end

end


The memcached gem will allow you to shove arbitrary Ruby objects into it. This should all get handled for you transparently, if you're using it.

Otherwise, take a look at ActiveRecord::Base#instantiate to see how it's done normally. You're going to have to trace through a bunch of rails stack, but that's what you get for attempting such hackery!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜