Render :json with includes 不渲染具有不同模型名称的 table 数据
Render :json with includes does not render table data with different model name
我有以下模型结构
class SetBenchmark < ApplicationRecord
self.table_name = 'benchmarks'
has_many :my_question_sets
end
class MyQuestionSet < ApplicationRecord
belongs_to :set_benchmark, class_name: 'SetBenchmark',
end
class MyAdmin < ApplicationRecord
has_many :my_question_sets
end
以下是控制器渲染方法
render json: my_admin, include: { my_question_sets: [ 'benchmark.*'] }
我没有收到 benchmark
响应数据。是因为它的 class 名称与 table 名称不同吗?
我认为您应该使用关联名称,而不是 json 规范中的 table 名称。
render json: my_admin, include: { my_question_sets: :set_benchmark }
https://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html#method-i-as_json
我有以下模型结构
class SetBenchmark < ApplicationRecord
self.table_name = 'benchmarks'
has_many :my_question_sets
end
class MyQuestionSet < ApplicationRecord
belongs_to :set_benchmark, class_name: 'SetBenchmark',
end
class MyAdmin < ApplicationRecord
has_many :my_question_sets
end
以下是控制器渲染方法
render json: my_admin, include: { my_question_sets: [ 'benchmark.*'] }
我没有收到 benchmark
响应数据。是因为它的 class 名称与 table 名称不同吗?
我认为您应该使用关联名称,而不是 json 规范中的 table 名称。
render json: my_admin, include: { my_question_sets: :set_benchmark }
https://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html#method-i-as_json