Help with endpoint for a non-ActiveRecord model without a database table
还没有人认领这个 Issue。
评估
调研方向
从 CalendarEvent 模型示例和 Api::V3::CalendarEventsController 开始,然后跟踪 JSONAPI::ResourceSerializer 如何处理映射后的 ActiveModel 数组。重现 204 No Content 响应,并检查 resource 和 serializer 对非 ActiveRecord 模型的行为。完成的标准是确定一种受支持的序列化方式,或记录所需的集成更改。
由索引模型根据 Issue 内容生成。
描述
I have a non-ActiveRercord model without a database table I am trying to create an API endpoint for. The data for this model is populated dynamically with custom SQL and provides generic relations to real ActiveRecords.
Because of all the magic happening with JSONAPI Resources I am having a hard time serializing the array of ActiveModels successfully. Am wondering what the best approach for this might be? I've tried some monkey patching without success and would appreciate guidance
model example
class CalendarEvent
include ActiveModel::Model
attr_accessor(
:id,
:title,
# etc etc
)
def self.modelize(record)
return CalendarEvent.new(record)
end
def self.records(person_id, start_date, end_date, options = {})
sql = "select 'some custom sql' from various_table where serious_magic_happens"
records = ActiveRecord::Base.connection.execute(sql)
records = records.map{ |r| self.modelize(r) } # returns an Array of CalendarEvent::Class
end
end
controller attempt
class Api::V3::CalendarEventsController < Api::V3::ApiController
def index
records = CalendarEvent.records(1686, '2017-07-01', '2017-07-31')
resources = records.map { |r| Api::V3::CalendarEventResource.new(r, nil) }
JSONAPI::ResourceSerializer.new(Api::V3::CalendarEventResource).serialize_to_hash(resources)
end
end
The above controller returns a 204 No Content even though I am passing serialize_to_hash an array ... am I misunderstanding how this should work ? Or is there a better way?
Thanks!
- 主要语言
- Ruby
- 星标
- 2.3k
- 派生
- 546
- PR 合并指标
- 30 天内没有已合并 PR
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
JSONAPI-Resources/jsonapi-resources 的其他 Issue
-
难度 1/5 1 小时以内 新手友好度 85/100
-
难度 3/5 1-2 天 新手友好度 50/100
JSONAPI-Resources/jsonapi-resources#1488 · 1 条评论 ·
-
难度 4/5 3-5 天 新手友好度 42/100
JSONAPI-Resources/jsonapi-resources#1479 · 2 条评论 ·
-
难度 4/5 3-5 天 新手友好度 30/100
JSONAPI-Resources/jsonapi-resources#1477 · 3 条评论 · 1 个 reaction ·
-
难度 3/5 1-2 天 新手友好度 45/100
查看 JSONAPI-Resources/jsonapi-resources 的全部 Issue
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 82/100
hanami/hanami-cli#449 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
resque/resque-scheduler#826 ·
-
bug
难度 2/5 1-3 小时 新手友好度 68/100
-
难度 2/5 1-3 小时 新手友好度 65/100
-
难度 2/5 1-3 小时 新手友好度 75/100