This question already has answers here:
How to convert records including 'include' associations to JSON
(2 answers)
Closed 9 years ago.
I'm trying to store the results of a model and an association in a variable like so:
data = Sale.all.includes(:book)
However, all this does is store active record objects for Sale in data with no data on the associated Book (each Sale has_one Book)
So, I tried:
Sale.all.includes(:book).map |sale|
puts sale.book
puts sale
end
Which gives me exactly what I'm looking for. However I need all of this store inside of data variable.
What is the best way to get the sale data along with the book data (for each sale) into some sort of hash or JSON or XML?
Hope I'm explaining this well enough
You can use the as_json that returns a Hash and accepts some additional serialization options.
data = Sale.all.includes(:book).as_json(include: [:book])
Related
This question already has answers here:
Nested hash in redis
(2 answers)
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
I want to get a data structure like this after the sql query:-
Users table has username and city. It has 10000 records
{
"cities_arry": {
"NY": ["john", "Mich", "Roh", "Dh", "Vir"],
"KL": ["Big", "ching", "qull"],
...
}
}
update: it is not possible to store nested hash on redis. So have to use MongoDb or some other tool.
Suppose you have a table users and you have city and username, and you want to find a data structure like above, then how would you approach
How to get faster query result to get data structure like this.
Redis doesn't support nested data structures, and specifically it doesn't support a Hash inside a Hash :) You basically have a choice between two options: either serialize the internal Hash and store it in a Hash field or use another Hash key and just keep a reference to it in a field of the outer Hash.
I am quite a newbie to Cube.js. I have been trying to integrate Cube.js analytics functionality with my Ruby on Rails app. The database is PostgreSQL. In a database, there is a certain column called answers_json with jsonb data type which contains a nested hash. An example of data of that column is:
**answers_json:**
"question_weights_calc"=>
{"314"=>{"329"=>1.5, "331"=>4.5, "332"=>1.5, "333"=>3.0},
"315"=>{"334"=>1.5, "335"=>4.5, "336"=>1.5, "337"=>3.0},
"316"=>{"338"=>1.5, "339"=>3.0}}
There are many more keys in the same column with the same hash structure as shown above. I posted the specific part because I would be dealing with this part only. I need assistance with accessing the values in the hash. The column has a nested hash. In the example above, the keys "314", "315" and "316" are Category IDs. The keys associated with Category ID "314" are "329","331","332", "333"; which are Question IDs. Each category will have multiple questions. For different records, the category and question IDs will be dynamic. For example, for another record, Category ID and Question IDs associated with that category id will be different. I need to access the values associated with the key question id. For example, to access the value "1.5" I need to do this in my schema file:
**sql: `(answers_json -> 'question_weights_calc' -> '314' ->> '329')`**
But the issue here is, those ids will be dynamic for different records in the database. Instead of "314" and "329", they can be some other numbers. Adding different record's json here for clarification:
**answers_json:**
"question_weights_calc"=>{"129"=>{"273"=>6.0, "275"=>15.0, "277"=>8.0}, "252"=>{"279"=>3.0, "281"=>8.0, "283"=>3.0}}}
How can I know and access those dynamic IDs and their values since I also need to perform mathematical operations on values. Thanks!
As a general rule, it's difficult to run SQL-based reporting on highly dynamic JSON data. Postgres does have some useful functions for dealing with JSON, and you might be able to use json_each or json_object_keys plus a few joins to get there, but its quite likely that the performance and maintainability of such a query would be difficult to say the least 😅 Cube.js ultimately executes SQL queries, so if you do go the above route, the query should be easily transferrable to a Cube.js schema.
Another approach would be to create a separate data processing pipeline that collects all the JSON data and flattens it into a single table. The pipeline should then store this data back in your database of choice, from where you could then use Cube.js to query it.
If I want to have a record in Rails that has many options, such as a selection of times, then what is the best method for storing this in the record? I don't really want to setup the times as objects and use has_many, since 12:17pm doesn't really make sense as an object. What do I use to put a list of variable size into an ActiveRecord?
As an example, consider a DB of videos in a video collection. And for each video we want a list of all the dates that it was watched. We also have no need to search dates across all the videos, we just want to be able to list and maybe edit the dates for a given video object.
I understand why it seem unaesthetic to have the Times as their own database table but it might be the easiest option.
Database tables often get created which aren't pretty - join tables, for examples. Perhaps the model could be called TimeOption and it would have a foreign key. Seems like you already understand how to do, this though.
I'm also assuming the options are dynamic, otherwise they can just be stored in an application constant.
Another option is to use Postgres' array data type, which is new but I haven't used.
Another option is to serialize the array of Times into json, yaml, or csv and then store the serialized string in the database. Deserialize the data to read it and reserialize to save.
This question already has answers here:
Core Data with Array Attribute
(2 answers)
Closed 7 years ago.
Please I really discouraged,
Is it possible to make a single entity like array attribute?
For example :
entity = person
attribute = courses
And the entity able to contain a few courses(attribute).
i have a many persons, and i need storing many courses on one person
and i don't know where to store them, and how store them
and after to get the specific person and show the all courses...
It would be a more common design to make both person and course entities and then have a many-to-many relationship between them. That way, if something about a course changes, you don't have to make the change in every person's array.
This question already has an answer here:
CoreData - one-to-many modeled relationship comes out as one-to-one
(1 answer)
Closed 8 years ago.
I am new to Core Data and am in the process of integrating it into an existing iPhone application. Here is my question:
Along with the standard data types available for entity attributes, is there any way to store multiple values for an attribute, such as a list? Or can I create some custom list class and have it available as an attribute data type?
Thanks, Viv!
Along with the standard data types available for entity attributes, is
there any way to store multiple values for an attribute, such as a
list?
If you have multiple values to store for a single property, make a separate entity that holds one instance of such a value and use a to-many relationship.
If you want to have a list type attribute, of other records to store in core data, you can use a too many relationship. Otherwise, if you just want a custom style attribute, it is possible to wrap any class inside of an NSData object and then store it as a Binary Data Attribute. This being said, you will not be able to use a fetch request to search the database using properties of this data, just whether or not it exists.