in Education by
I have a field call query_data defined as text in my MySQL database. In my model I defined this field as serialize :query_data, JSON. The JSON format I would like to save and retrieve look like that: {:items => [ {:id => 1}, {:id => 2}, {:id => 3} ]} I have a collection (in that case, called items) that contain an array of objects. I was wondering, what's the best way to add or delete an Item. Ex: remove {:id => 2} from my items list and add `{:id => 4} to it JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Ruby on Rails has some nice methods to move seamlessly between JSON and Ruby. thing = {:items => [ {:id => 1}, {:id => 2}, {:id => 3} ]} thing.to_json # "{\"items\":[{\"id\":1},{\"id\":2},{\"id\":3}]}" thing.to_json is essentially what's happening in the serializer. If you want them back to Ruby, you can just do: @items = @thing.query_data JSON.parse(@items) # "items"=>[{"id"=>1}, {"id"=>2}, {"id"=>3}]} Now that we can easily move between the two, lets just use Ruby syntax to deal with adding and deleting keys. thing = {:items => [ {:id => 1}, {:id => 2}, {:id => 3} ]} thing[:items] = thing[:items].append({:id => 4}) # adding a new item thing[:items] = thing[:items].select { |item| item[:id] != 2 } # removing an item

Related questions

0 votes
    How to add an element into an Array?...
asked Nov 30, 2020 in Technology by JackTerrance
0 votes
    I would like to change the user sign-in behaviour of devise in a way to redirect a user to a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I have several properties that are dependent on the environment the application is running. For example, there ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 20, 2022 in Education by JackTerrance
0 votes
    ActiveRecord 3.2.14 I want to use ActiveRecord in a non-Rails Ruby project. I want to have ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 3, 2022 in Education by JackTerrance
0 votes
    The RoR tutorials posit one model per table for the ORM to work. My DB schema has some 70 tables ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    I have been trying to use routes.rb for creating a URL /similar-to-:product (where product is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 23, 2022 in Education by JackTerrance
0 votes
    What is current state of the art for enabling OpenID login in Ruby on Rails applications? This is a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I have been trying to use routes.rb for creating a URL /similar-to-:product (where product is ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    I need to Separate multiple Array entries with a newline. the names in the aray value. Again I want ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I try to merge some object into one array I did output by q = [["99","99","99"],["9" ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 1, 2022 in Education by JackTerrance
0 votes
    how to push an element into an array in javascript?...
asked Dec 10, 2020 in Technology by JackTerrance
0 votes
    I moved a web app that was using 1.8.7 to 1.9.2 and now I keep getting incompatible character ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    write a program to print fibonacci series . write a program to search an element within the array using binary search Select the correct answer from above options...
asked Dec 29, 2021 in Education by JackTerrance
0 votes
    Given an array of size n, let's assume an element is touched' if and only if some operation is ... answer from above options Data Structures and Algorithms questions and answers...
asked Nov 14, 2021 in Education by JackTerrance
...