Rails 4.2.6 Problems with maintaining JSON Postgresql



  • Strong Parametrs:

    params.require(:author).permit(:firstname, :secondname, :patronymic, :position, :data)
    

    Field data - has a type JSON, OBD - PG

    If retained/renewed, the field remains empty

    I tried to write:

      props = params.require(:author).permit(:firstname, :secondname, :patronymic, :position, :data)
      props[:data] = params[:author][:data]
    

    And so:

      props = params.require(:author).permit(:firstname, :secondname, :patronymic, :position, :data)
      props[:data] = params[:author][:data].to_hash
    

    Trying to make an association, and naturally nothing works. I sold an envelope to json, but it's still a line:

      props = params.require(:author).permit(:firstname, :secondname, :patronymic, :position, :data)
      props[:data] = params[:author][:data].to_json
    

    The postgres is the type of data.

    It's a lot to choose in the control room. But there is a need to store data, with dynamic fields and a sample.

    Thank you for your help!



  • Such cases http://guides.rubyonrails.org/action_controller_overview.html#outside-the-scope-of-strong-parameters ♪

    If you know the permitted keys in advance. Scales (single values, kashmaps and masses) not() In the Hashmap invested, you may indicate clearly:

    params.require(:author)
          .permit(:firstname, :secondname, :patronymic, :position,
                  data: [:one, :two, :three])
    

    ...and if you don't, and you just want to say anything, then Strong Parameters isn't your assistant anymore. He's invented for typical cases where there's a white list of keys. So you're gonna have to squeeze the results of his job, make a sample of the keys you know, and then throw in the data files that you don't care about. For example:

    author = params.require(:author)
    

    author.permit(:firstname, :secondname, :patronymic, :position)
    .merge(author.slice(:data)) # <= "выщипывает" ключ-значение только для :data

    ^^^^^ как обычный Hash#merge


    For reasons of comment: Remember what's for json operator = No. Therefore, search for a whole field data I won't work. find_or_create♪ https://ru.stackoverflow.com/questions/503916/rails-validates-uniqueness-%D0%B4%D0%BB%D1%8F-postgresql-json-type Nowhere. It exists. jsonbbut it's a different story.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2