0
DBとしてポストグル(9.6)のあるレール(5.1)アプリケーションがあります。テーブルの1つでは、jsonbフィールドがあり、必要なのは、この文字列を取得してクライアントに送信することだけです。なぜ私はそれが必要なのですか? JSONに変換すると、私は2つのソリューションを見つけデータベースからjsonを取り出すことは可能ですか?
my_record.my_json_field # returns hash
my_record.read_attribute(:my_json_field) # also returns hash
# even this hack returns hash
MyRecord.select('my_json_field as temp_field').first[:temp_field]
合計要求時間の半分を取ります
を試してみてください(を働いたコメントからの提案を、移植)
# proposed by Sergio Tulentsev
# It's a fastest way. you will receive just a hash with fields that you selected
MyRecord.connection.execute(MyRecord.my_scope.to_sql)
# Another option is to select that data as a text.
# In that case you will receive ActiveRecord objects
MyRecord.select('*', 'my_json_field::text as my_field_as_text').first.my_field_as_text