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:
- Fetch an existing record from the database.
- Store as “hashed” output of the record into redis or some other memory store.
- Next time when that record is fetched, fetch the cached store first and if it is not found then goto step 1.
- 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.
Create a new model class which extends the model class that I want to have cached into memory.
Set the table_name of the new class to the same one as the parent class.
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.
Overload the method
new_record?and set that tofalseso that the associations work.Here’s my code: