Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Help with endpoint for a non-ActiveRecord model without a database table

未关闭
#1,092 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
20/100
Issue 类型
缺陷
描述清晰度
需要澄清
活跃度
停滞
技术栈
rails, ruby
领域
api, backend

调研方向

从 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

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

JSONAPI-Resources/jsonapi-resources 的其他 Issue

查看 JSONAPI-Resources/jsonapi-resources 的全部 Issue

相似的 Issue

更多 Ruby Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。