Storing large objects for a user session in Rails - ruby-on-rails

I'm making use of Google API client gem which returns data and is then wrapped in the an object. I want to use this data between sessions. How should I store it? I thought at first I could serialize it then put it into a table, but ran into problems with the type of object it is an gave up. Actually thereafter I thought it would be neater to take the data I need and put it into an array of hashes (eg. for youtube videos - title, id, thumbnail etc) so I extracted what I needed and done so. Now, I have an array - should I store it in the database for the user or should I put it into a session var? Session var seems easier, no need to create an additional table etc but does that mean it will be stored as a cookie? Might be a rather large cookie then. Anyway advise here would be much appreciated.

First, with database storage: with ActiveRecord you can simply store JSON. So, if your object can be serialized correctly, than there are no problems to store it in DB. just add this line to your model:
serialize :data, JSON.
Though, I think you can perfectly use session var for that purpose. By default, yes, it will be stored as a cookie - but if you think it's too much information, you can configure rails to use server-side storage instead.

Related

Persist a single variable in rails

I want to store a single variable across all my users in a rails application.
This variable is just an integer that can change sometimes.
I want it to persist across different launch of the server.
I want any controller to be able to change (and persist) the value
It sounds really easy to do, but I can't find any tool that would do it for me...
I would create a Key/Value table and model.
So far, it would only store one key, the integer you're talking about. Later, it could do a little more. The value could be a JSON document so you can store any kind of JSON supported native types very easily.
Simply create a model with one attribute. And then create one instance of that model. Then access this from your controllers
for example
Model.first.attribute
I ended up using the gem rails-settings-cached
It provide an easy way to access a persistant variable:
Setting.a_setting = 42
Setting.a_setting #42

Store an array of hashes in a db Rails

I have an array of hashes from an api that I want to store in a database. This data is just a list of names that are always going to be the same so I wanted to remove the api call to reduce load times. How can I store this data? Do I have to make a model of it?
The data looks like this:
[{"id":"30","name":"Mike"},
{"id":"57","name":"Dave"},
{"id":"9","name":"Kevin"},
....
{"id":"20","name":"Kyle"}]
Would I be able to open a db viewer/editor tool and just type the data in?
It looks to be structured in a way that would make it very suitable as a new model in the application. You don't have to do that, but for the most conventional storage form, that would be my choice.

Store data between action

What the best way to store data between two controller action?
Example:
I have a big array of posts id ["2907", "2334", "2309",.... N] i create it in first step confirm and go to second step to another controller action.
Another controller action render data (calendar, groups), i select date in calendar, choose group and create one more array of date-time data confirm and go to another action
Another action manipulate with this array and array of posts id, and render date again the data what i whant to save after confirm, data safe in DB and clear all array from session.
I store all array between action in session like session[:posts_ids] = params[:posts_ids] and flash[:date_day] ||= params[:date_day]. I think it do not work for a really big data array. May be the best way store it in redis ?
This is largely opinion-based, but I think your two best options are:
Use Redis, Memcached or some other server-side cacheing solution to store the objects in memory until you need them.
Make your app more ajax-y and do all of the selection and temporary
persistence of those arrays client-side.
Session size is limited by the underlying strategy, rather than a hard limit for sessions in general. CookieStore is the default and will allow ~4k of data, because browser cookies are limited to that size. If you use database-backed sessions, you're only limited by your database server.
You should also keep in mind that using sessions to store data between requests isn't very RESTful.

Store a session information - one more Core Data table or separate session-file in "key-value" format (YAML, JSON)?

I have a Session class which I want to hold and store app's current session information (like lastLocation, remoteSessionToken, ...)
I have default Core Data setup with a bunch of tables and the most obvious solution I see is to create one more table "session" with two string fields: 'field' and 'value' and store session information to this table in way like I store any information to other tables
'field' 'value'
lastLocation 46.68,34.18
removeSessionToken au987asdv7tta487tv9b
...
Also, I know I could have this done using a special separate file for settings fx in YAML or JSON format but this would lead to a logical inconsistency with having both Core Data database and a file.
Is there any recommended approach for holding and persisting session information?
Unless something like the following applies, put it in user defaults or in a special purpose file in the documents directory:
You have many sessions and need to search or filter them based on some kind of predicate
You have more than one session and sessions have some kind of relationship to specific objects in the data store.
It's not a "logical inconsistency" to use Core Data and separate files. Do what makes sense. It's not likely to make sense to keep this information in your data store, so don't do it unless you have some compelling reason.
If the information could be considered sensitive, put it in the keychain.
Also, it's extremely ugly to create a generic container entity in Core Data. Using field and value fields like you describe is not a good design decision.
As long as it's not sensitive information, I would be consistent and use Core Data. A more obscure place would be to put it in NSUserDefaults.

appropriate functions to override when executing subquery on encrypted binary data

I'm considering encrypting every attribute in a sqlite data model. Encrypting the data clearly complicates accessing attribute values, saving attribute values, and searching for attribute values from the perspective of the view (in MVC).
I was thinking it may suffice to override NSFetchedResultsController methods fetchedResults and performFetch:, and NSManagedObjectContext method save:
I have also gotten into loads of trouble by assuming it was okay to override in the past.
Since this will likely take some time to work through all the details, I would like to know if anyone has discovered (and verified) an appropriate way to add codec mechanisms such that it's not necessary to encode and decode at every place in code where I search, access and save persistent data? And if so, what was the approach?

Resources