mercredi 26 août 2015

Serializing model with multiple primary keys

I have a Rails API that communicates with an Ember app. I need to be able to serialize a model that has two primary keys instead of the default id.

This is what it looks like right now:

Models

# This is the problem model:
class Value < ActiveRecord::Base
  self.primary_keys = :foreign_id, :type_id
  belongs_to :type, foreign_key: :type_id
end

class Type < ActiveRecord::Base
  has_many :codes, foreign_key: :code_type_id
  has_many :values, foreign_key: :type_id
end

class Code < ActiveRecord::Base
  belongs_to :type, foreign_key: :code_type_id
end

class Activity < ActiveRecord::Base
  has_many :values, -> (object){ where("type_subject_area = ?", "Activity") }, foreign_key: :foreign_id
end

Schema

create_table "values", id: false, force: :cascade do |t|
  t.integer  "foreign_id"
  t.string   "type_subject_area"
  t.integer  "code_id"
  t.integer  "type_id"
  t.string   "type_title"
  t.text     "text"
  t.datetime "created_at"
  t.datetime "updated_at"
end

add_index "values", ["foreign_id", "type_id"], name: "index_values_on_foreign_id_and_type_id", unique: true, using: :btree

create_table "types", force: :cascade do |t|
  t.integer  "id"
  t.string   "title"
  t.string   "subject_area"
  t.datetime "created_at"
  t.datetime "updated_at"
end

add_index "types", ["id"], name: "index_types_on_id", unique: true, using: :btree

create_table "activities", force: :cascade do |t|
  t.integer  "id"
  t.datetime "created_at"
  t.string   "name"
end

add_index "activities", ["activity_type"], name: "index_activities_on_activity_type", using: :btree
add_index "activities", ["id"], name: "index_activities_on_id", unique: true, using: :btree

create_table "codes", force: :cascade do |t|
  t.integer  "id"
  t.string   "description"
  t.string   "code_value"
  t.integer  "code_type_id"
  t.datetime "created_at"
  t.datetime "updated_at"
end

add_index "codes", ["id"], name: "index_codes_on_id", unique: true, using: :btree

Serializers

class ActivitySerializer < ActiveModel::Serializer
  attributes  :id, :name
  has_many :values
end

class UDFValuesSerializer < ActiveModel::Serializer
  attributes :foreign_id, :type_subject_area, :type_id, :text, :type_title
end

As you can see, a type has many values and codes. An activity has many values, though it's only related to the values that have a subject area of "Activity". Since values don't have an individual primary key, I'm not sure how to format it in such a way that works in Ember. From what I understand, Ember needs to have an id available in order to receive the values.

Thoughts? Ideas? I'm afraid this needs to be in this format because the Rails API is communicating and syncing with another, larger API.




Aucun commentaire:

Enregistrer un commentaire