Rails.logger.debug "Full API result: #{result.inspect}"
- # Always store the API response and timestamp
- @post.update(
- summarizer_api_response: {
- response_body: result[:raw_response],
- status_code: result[:status_code],
- timestamp: Time.current
- },
- last_summarized_at: Time.current
- )
+ # Prepare the API response for storage
+ begin
+ # Construct a basic hash with stringified values
+ api_response = {
+ response_body: result[:raw_response].to_s, # Ensure string
+ status_code: result[:status_code].to_s, # Convert to string
+ timestamp: Time.current.iso8601
+ }
+
+ # Convert to JSON string explicitly
+ json_response = api_response.to_json
+
+ Rails.logger.debug "Attempting to store JSON response: #{json_response}"
+
+ # Store the JSON string
+ success = @post.update(
+ summarizer_api_response: json_response,
+ last_summarized_at: Time.current
+ )
+
+ if !success
+ Rails.logger.error "Failed to update post with API response: #{@post.errors.full_messages}"
+ end
+ rescue StandardError => e
+ Rails.logger.error "Error storing API response: #{e.message}"
+ Rails.logger.error e.backtrace.join("\n")
+ # Store a simplified error response
+ error_json = {
+ error: e.message,
+ timestamp: Time.current.iso8601
+ }.to_json
+
+ @post.update(
+ summarizer_api_response: error_json,
+ last_summarized_at: Time.current
+ )
+ end
if result[:success] && result[:parsed_response]
summary_text = result[:parsed_response]['output'] ||
--- /dev/null
+class FixApiResponseColumn < ActiveRecord::Migration[7.2]
+ def up
+ # First, remove any existing column to ensure clean state
+ remove_column :posts, :summarizer_api_response if column_exists?(:posts, :summarizer_api_response)
+
+ # Then add it back with proper JSON type for MySQL
+ add_column :posts, :summarizer_api_response, :text
+
+ # Add a check constraint to ensure valid JSON (MySQL 8.0.17+)
+ execute <<-SQL
+ ALTER TABLE posts
+ ADD CONSTRAINT check_valid_json
+ CHECK (
+ summarizer_api_response IS NULL OR
+ JSON_VALID(summarizer_api_response)
+ )
+ SQL
+ end
+
+ def down
+ remove_column :posts, :summarizer_api_response
+ end
+end
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema[7.2].define(version: 2024_12_08_065057) do
+ActiveRecord::Schema[7.2].define(version: 2024_12_08_070031) do
create_table "api_keys", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
t.string "key"
t.datetime "created_at", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "summary"
- t.json "summarizer_api_response"
t.datetime "last_summarized_at"
+ t.text "summarizer_api_response"
t.index ["post_type"], name: "index_posts_on_post_type"
t.index ["published_at"], name: "index_posts_on_published_at"
t.index ["slug"], name: "index_posts_on_slug", unique: true
+ t.check_constraint "(`summarizer_api_response` is null) or json_valid(`summarizer_api_response`)", name: "check_valid_json"
end
create_table "users", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|