【问题标题】:Ruby on Rails Grape API omitting attributesRuby on Rails Grape API 省略属性
【发布时间】:2019-12-18 10:04:18
【问题描述】:

我正在 RoR 中构建一个葡萄 API,并且我正在尝试为我的模型创建一个 GET 端点,但是 user_id、created_at 和 updated_at 属性与实际的 JSON 对象截断了。

这是我的终点:

resource :pairings do
  desc 'Gets all pairings from database'
  get do
    Pairing.all
  end
end

配对模型如下所示:

create_table "pairings", force: :cascade do |t|
  t.string "dumb_unit_id", null: false
  t.string "smart_unit_id", null: false
  t.datetime "created_at", precision: 6, null: false
  t.datetime "updated_at", precision: 6, null: false
  t.integer "user_id", null: false
  t.index ["dumb_unit_id"], name: "index_pairings_on_dumb_unit_id", unique: true
  t.index ["smart_unit_id"], name: "index_pairings_on_smart_unit_id", unique: true
end

在 API 的响应中,配对最终看起来像这样:

{"id"=>979, "dumb_unit_id"=>"wu5qg63k6c1iz", "smart_unit_id"=>"rjnc8yg53j29c"}

没有 user_id 或时间戳。

【问题讨论】:

    标签: ruby-on-rails grape-api


    【解决方案1】:

    我发现了问题。我有一个自动生成的序列化程序,它只定义了 :id、:dumb_unit_id 和 :smart_unit_id 作为属性。在这个文件中添加 :user_id 作为属性解决了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-14
      • 1970-01-01
      • 2011-04-01
      • 2014-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多