Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

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

Đang mở
#1,092 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
20/100
Loại issue
Lỗi
Độ rõ ràng
Cần làm rõ
Mức độ hoạt động
Đình trệ
Công nghệ
rails, ruby
Lĩnh vực
api, backend

Hướng nghiên cứu

Bắt đầu với ví dụ về model CalendarEvent và Api::V3::CalendarEventsController, sau đó theo dõi cách JSONAPI::ResourceSerializer xử lý mảng ActiveModel đã được ánh xạ. Tái hiện response 204 No Content và kiểm tra hành vi của resource và serializer đối với các model không phải ActiveRecord. Hoàn tất khi xác định được một phương pháp serialization được hỗ trợ hoặc ghi lại các thay đổi tích hợp cần thiết.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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!

Ngôn ngữ chính
Ruby
Star
2.3k
Fork
546
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của JSONAPI-Resources/jsonapi-resources

Tất cả issue của JSONAPI-Resources/jsonapi-resources

Issue tương tự

Thêm issue về Ruby

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.