Existing Rails model without fetching it from the database

一个人想着一个人 提交于 2019-12-23 02:28:42

问题


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?


EDIT

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.


回答1:


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



回答2:


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!



来源:https://stackoverflow.com/questions/6638734/existing-rails-model-without-fetching-it-from-the-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!