1

大まかに言うと、RecipeSkill、およびがありUser、 と の結合テーブルがRecipeSkillありUserSkillます。

特定のレシピのスキルを返すとき、ユーザーがそのレシピのどのスキルを既に習得しているかを知りたいです。JSON の例を以下に示します。

この問題への取り組み方に迷っているだけなので、この質問をどのように表現するのが最善なのかさえよくわかりません。一緒に何かをハックできると確信していますが、既存の慣習が必要な場合はかなり一般的なようです。

これが私のモデルと私のものRecipeSerializerです:

class Recipe < ActiveRecord::Base
  has_many :recipe_skills
  has_many :skills, through: :recipe_skills
end

class Skill < ActiveRecord::Base
  has_many :recipe_skills
  has_many :recipes, through: :recipe_skills
end

class RecipeSkill < ActiveRecord::Base
  belongs_to :recipe
  belongs_to :skill
end

class User < ActiveRecord::Base
  has_many :user_skills
  has_many :skills, through: :user_skills
end

class UserSkill < ActiveRecord::Base
  belongs_to :user
  belongs_to :skill
  # attributes :id, :user_id, :skill_id, :strength, :capacity, :learned
end

class RecipeSerializer < ActiveModel::Serializer
  embed :ids, include: true

  has_many :skills

  attributes :id, :title
end

JSON の例を次に示します。

{
  "skills": [
    {
      "id": 1,
      "name": "Grilling Chicken",
      "earned": true
    }
  ]
  "recipe": {
    "id": 1,
    "title": "Roasted Potatoes",
    "skill_ids": [
      1
    ]
  }
}
4

1 に答える 1