将嵌套关联呈现为 json
Render nested associations as json
以下是我的模型定义
# GoogleApp model
class GoogleApp < ApplicationRecord
has_many :gmail_users, foreign_key: 'app_account', primary_key: 'google_account', dependent: :destroy
has_many :gmail_threads, foreign_key: 'app_account', primary_key: 'google_account', dependent: :destroy
end
# GmailUser model
class GmailUser < ApplicationRecord
belongs_to :google_app, foreign_key: 'app_account', primary_key: 'google_account', optional: true
has_one :gmail_thread, foreign_key: 'user_id', primary_key: 'user_id', dependent: :destroy
end
# GmailThread model
class GmailThread < ApplicationRecord
belongs_to :google_app, foreign_key: 'app_account', primary_key: 'google_account', optional: true
belongs_to :gmail_user, foreign_key: 'user_id', primary_key: 'user_id', optional: true
has_many :gmail_messages, -> { order "sent_at asc" }, foreign_key: 'thread_id', primary_key: 'thread_id', dependent: :destroy
end
# GmailMessage model
class GmailMessage < ApplicationRecord
belongs_to :gmail_thread, foreign_key: 'thread_id', primary_key: 'thread_id', optional: true
has_many :gmail_attachments, foreign_key: 'message_id', primary_key: 'message_id', dependent: :destroy
end
# GmailAttachment model
require "base64"
class GmailAttachment < ApplicationRecord
belongs_to :gmail_message, foreign_key: 'message_id', primary_key: 'message_id', optional: true
def encode_as_base64_data
Base64.encode64(self.base64_data)
end
end
我正在尝试 return 具有以下嵌套关联的 GoogleApp 实例
render status: 200, json: @google_app.to_json({include: [gmail_users: {include: [gmail_thread: {include: [gmail_messages: {include: {gmail_attachments: (except: :base64_data, methods: [:encode_as_base64_data])}}]}]}]})
但这在 except
上给出了语法错误,即
SyntaxError (/home/work/projects/backend/app/controllers/gmail_controller.rb:40: syntax error, unexpected tLABEL
...e: {gmail_attachments: (except: :base64_data, methods: [:enc...
... ^~~~~~~
):
我没能解决这个语法错误问题。当我这样做时
render status: 200, json: GmailAttachment.first.to_json(except: [:base64_data], methods: [:encode_as_base64_data])
然后它按预期工作。
exclude
应该换成 {}
而不是 ()
render status: 200, json: @google_app.to_json({include: [gmail_users: {include: [gmail_thread: {include: [gmail_messages: {include: {gmail_attachments: {except: :base64_data, methods: [:encode_as_base64_data]}}]}]}]})
我建议使用 jbuilder Gem 以可维护的方式包含嵌套模型。
以下是我的模型定义
# GoogleApp model
class GoogleApp < ApplicationRecord
has_many :gmail_users, foreign_key: 'app_account', primary_key: 'google_account', dependent: :destroy
has_many :gmail_threads, foreign_key: 'app_account', primary_key: 'google_account', dependent: :destroy
end
# GmailUser model
class GmailUser < ApplicationRecord
belongs_to :google_app, foreign_key: 'app_account', primary_key: 'google_account', optional: true
has_one :gmail_thread, foreign_key: 'user_id', primary_key: 'user_id', dependent: :destroy
end
# GmailThread model
class GmailThread < ApplicationRecord
belongs_to :google_app, foreign_key: 'app_account', primary_key: 'google_account', optional: true
belongs_to :gmail_user, foreign_key: 'user_id', primary_key: 'user_id', optional: true
has_many :gmail_messages, -> { order "sent_at asc" }, foreign_key: 'thread_id', primary_key: 'thread_id', dependent: :destroy
end
# GmailMessage model
class GmailMessage < ApplicationRecord
belongs_to :gmail_thread, foreign_key: 'thread_id', primary_key: 'thread_id', optional: true
has_many :gmail_attachments, foreign_key: 'message_id', primary_key: 'message_id', dependent: :destroy
end
# GmailAttachment model
require "base64"
class GmailAttachment < ApplicationRecord
belongs_to :gmail_message, foreign_key: 'message_id', primary_key: 'message_id', optional: true
def encode_as_base64_data
Base64.encode64(self.base64_data)
end
end
我正在尝试 return 具有以下嵌套关联的 GoogleApp 实例
render status: 200, json: @google_app.to_json({include: [gmail_users: {include: [gmail_thread: {include: [gmail_messages: {include: {gmail_attachments: (except: :base64_data, methods: [:encode_as_base64_data])}}]}]}]})
但这在 except
上给出了语法错误,即
SyntaxError (/home/work/projects/backend/app/controllers/gmail_controller.rb:40: syntax error, unexpected tLABEL ...e: {gmail_attachments: (except: :base64_data, methods: [:enc... ... ^~~~~~~ ):
我没能解决这个语法错误问题。当我这样做时
render status: 200, json: GmailAttachment.first.to_json(except: [:base64_data], methods: [:encode_as_base64_data])
然后它按预期工作。
exclude
应该换成 {}
而不是 ()
render status: 200, json: @google_app.to_json({include: [gmail_users: {include: [gmail_thread: {include: [gmail_messages: {include: {gmail_attachments: {except: :base64_data, methods: [:encode_as_base64_data]}}]}]}]})
我建议使用 jbuilder Gem 以可维护的方式包含嵌套模型。