Okay, I know this is going to sound like a terrible idea to many of you, but please bear with me for just a moment. I want to be able to have a few of my models optionally persist to my session instead of to my database under certain circumstances. Basically I want to be able to provide a limited demo version of my site to potential new users without polluting my db with all their fooling around. The sessions are expected (maybe enforced) to be short-lived and contain only a small number of model instances. But other than the fact that their data will all disappear when their session expires, the site should behave fully identical with the normal site. Does anyone have any clever suggestions on how i can trick rails into doing something like this? Basically I want to be able to persist to either a session or a db depending on the user. Mike _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Francois Beausoleil
2005-Oct-18 17:05 UTC
Re: persisting entities to session instead of db
Hello Michael, Wrong list, but I'll reply anyway. 2005/10/17, Michael Burton <mb-list@niskala.org>:> I want to be able to have a few of my models optionally persist to my > session instead of to my database under certain circumstances. Basically I > want to be able to provide a limited demo version of my site to potential > new users without polluting my db with all their fooling around. The > sessions are expected (maybe enforced) to be short-lived and contain only a > small number of model instances. But other than the fact that their data > will all disappear when their session expires, the site should behave fully > identical with the normal site. > > Does anyone have any clever suggestions on how i can trick rails into doing > something like this? Basically I want to be able to persist to either a > session or a db depending on the user.Add a flag to the session which tells you where to store the models. Then, add an helper method to your controllers which uses the flag and saves to the right place. There's no magic involved, and you use ActiveRecord just the way you normally do. A pragmatic solution. Bye, François _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Thank you Francois, I thought about this, but it didn''t appear to be sufficient because i would also need to override any find functions that i used as well. I also wasn''t sure how my many-to-many relationships would be handled by ActiveRecord either... would it know to retrieve the appropriate foreign keys from the db even though the parent record is in a session? What about special fields like created_on or auto-incrementing ids? Do they require special handling? I could probably track down all the places in my model that reference the db and switch them as you suggested (read_attribute, write_attribute, find*, ...? ), but i wasn''t sure I had the complete list. I figured it would probably be easier to override these methods in my own special subclass of ActiveRecord, but again thought it might be worth putting the question out to the community to see if there are any gotchas i should look out for. Thanks for your suggestion, do you have any further thoughts? Mike On 10/18/05, Francois Beausoleil <francois.beausoleil-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hello Michael, > > Wrong list, but I''ll reply anyway. > > 2005/10/17, Michael Burton <mb-list-2MT9zwG8tcBAfugRpC6u6w@public.gmane.org>: > > I want to be able to have a few of my models optionally persist to my > > session instead of to my database under certain circumstances. Basically > I > > want to be able to provide a limited demo version of my site to > potential > > new users without polluting my db with all their fooling around. The > > sessions are expected (maybe enforced) to be short-lived and contain > only a > > small number of model instances. But other than the fact that their data > > will all disappear when their session expires, the site should behave > fully > > identical with the normal site. > > > > Does anyone have any clever suggestions on how i can trick rails into > doing > > something like this? Basically I want to be able to persist to either a > > session or a db depending on the user. > > Add a flag to the session which tells you where to store the models. > Then, add an helper method to your controllers which uses the flag and > saves to the right place. > > There''s no magic involved, and you use ActiveRecord just the way you > normally do. A pragmatic solution. > > Bye, > François > > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs > > >_______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs