0

i have the same problem described in this question. PostgreSQL: compare jsons

but, i want a way to do it in rails, i have metadata column, that i have to query every time a record inserted. so, i have to compare between the value that will be inserted and the content i have, also, that column maybe empty.

what i have tried to use the json::text, but that means that i have to make a query for each setting of the json keys and values.

    ## for checking the empty query and if not empty, i loop through the json objects
    if metadata.to_s == '{}' || metadata.to_s == ''
      gears = gear_query.where("metadata::text = ?", metadata.to_s)
    else
      gears = gear_query.each.collect{ |g| g if g.eq_metadata?(metadata) }.compact
    end

    ## looping through all the shapes of the json objects 
    ## and calling diff function to recognize if the objects are different or not

  def eq_metadata?(metadata)
    src_mtd = self.metadata.sort_by { |k, v| k }.to_h
    new_mtd = metadata.sort_by { |k, v| k }.to_h
    src_keys = src_mtd.keys.each.map(&:to_sym)
    new_keys = new_mtd.keys.each.map(&:to_sym)
    ((src_keys == new_keys) && (src_mtd.values == new_mtd.values))
  end

i think that there is a better way, or that i am missing something with my code. my question not about raw sql queries, it is about a way to compare json objects' content stored in the database with a json object in hand. the way i took is to get all the json objects out of the database then comparing them to the json object in hand... is there another way??

icarus
  • 85
  • 2
  • 13
  • Possible duplicate of [Rails raw SQL example](https://stackoverflow.com/questions/14824453/rails-raw-sql-example) – Raymond Nijland Jan 01 '19 at 14:16
  • @RaymondNijland, thanks alot but my question not about raw sql queries, it is about a way to compare json objects' content stored in the database with a json object in hand. the way i took is to get all the json objects out of the database then comparing them to the json object in hand... is there another way?? – icarus Jan 02 '19 at 12:30
  • ", it is about a way to compare json objects' content stored in the database with a json object in hand" "is there another way?? " Probably not i think executiing a raw query on the active record framework is the only way. – Raymond Nijland Jan 02 '19 at 15:51
  • Okay. But the question you say that mine is a duplicate from is about raw queries in general, not about json objects comparison as in my question. – icarus Jan 02 '19 at 16:48
  • not **is** a duplicate i said a **possible** duplicate besides the comment is automated when you flag a post as duplication vote close.. "is about raw queries in general, not about json objects comparison as in my question" think for yourself please what programming requires and edit the duplication answer so following for example.... `sql = "select ('{"x":"a", "y":"b"}')::json::text = ('{"y":"b", "x":"a"}')::json::text" records_array = ActiveRecord::Base.connection.execute(sql)`; ... or with a table `select * from table WHERE ::json::text = ('{"y":"b", "x":"a"}')::json::text` – Raymond Nijland Jan 02 '19 at 17:37
  • @Raymond Nijland, ok, sorry, i got you! A very close answer is in the question i included at the beginning of my question. Thanks for your comment. And sorry again that in have got you not in a correct way. – icarus Jan 02 '19 at 18:11

0 Answers0