Reputation: 6346
I'm making an AJAX get request to one of my controller actions resposible for serving JSON to a javascript function that in turn generates an svg based on data inside my view.
However the data I'm working with also needs to be represented within a table inside of a view and I didn't want the query to have to be ran against the database more than once so I was wondering if it was possible just to perform the query on the initial request for the page and then pass it to my other controller using AJAX under the params hash?
To simplify the question heres an overview of the current work-flow:
Page Request -> Query -> View Served -> AJAX GET Request -> Query -> Generate SVG
What I want:
Page Request -> Query -> View Served -> AJAX GET Request -> Generate SVG
I thought I could simply pass the relation as a param in the AJAX request but instead of passing the relation as an ActiveRecord::Relation it passes it as a string:
"#<ActiveRecord::Relation:0x00000009137fc8>":String
I also tried storing it into a session, e.g:
session[:foo] = @bar
But I'm met with the following error message which leads me to believe sessions can't store relations?
can't dump hash with default proc
What is the preferred solution for this kind of work flow?
Upvotes: 0
Views: 1068
Reputation: 558
you can do this :
var relation = '<%= @criteria.where_values_hash.to_json.html_safe %>'
Then you can send this relation var in the ajax request in "data"
Upvotes: 0
Reputation: 14048
The ActiveRecord::Relation
is an internal Rails object, and it's not something that the "outside world" of JavaScript knows anything about. (Theoretically, you could serialize it, then deserialize it ... but that's just a bad idea :-). So no, that's not the right approach. You can save the object itself (the result of the query, not the ActiveRecord::Relation) in the session, although this is not generally good practice.
So then how can you accomplish what you want?
First, Rails tends to cache most database queries, so before you go optimizing something, make sure it needs optimizing.
Assuming it does, it seems like you're doing two separate things: displaying the results of the query, then making a different request (the AJAX request) that generates the SVG. Aren't these two entirely different methods (well, related in some ways, obviously), but different in form, and run at different times. This would suggest two different methods.
If they are really close -- that is, you have 90% of what you need in the first view, then you could also just add a hidden field or undisplayed value in the first view, and in the JS that fires your AJAX query the page (DOM) for the values that are needed to generate the SVG.
In some cases, I have seen people use data-
attributes in the HTML to hold attribute values in a more structured form that's easily accessible via JS, effectively serializing the full object into undisplayed data in the HTML.
But before you do this, make sure you're optimizing something that needs optimizing :-)
Upvotes: 1