1
I 가지고, RecipeSkill
및 UserSkill
의 표를 결합하여 높은 수준 Recipe
, Skill
및 User
에서.레일-API : 메타 데이터를 연관 번째 행은 테이블 조인
주어진 레시피에 대해 스킬을 반환 할 때, 사용자가 이미 배운 레서피에 대해 어떤 스킬을 알고 싶습니다. 아래에서 JSON 예제를 볼 수 있습니다.
저는이 문제를 해결하는 방법에 대해 잃어버린 느낌이 들기 때문에이 질문을 표현하는 가장 좋은 방법은 확실하지 않습니다. 나는 함께 뭔가를 해킹 할 수있을 것이라고 확신하지만, 기존의 관례가 있어야하는 경우가 꽤 흔한 것 같다. 몇 가지 예를 JSON
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
그리고 다음과 같습니다 :
Skill
시리얼에
{
"skills": [
{
"id": 1,
"name": "Grilling Chicken",
"earned": true
}
]
"recipe": {
"id": 1,
"title": "Roasted Potatoes",
"skill_ids": [
1
]
}
}
내 관심사는 실제로 성능 문제입니다. 더 좋은 방법이있을 수 있기를 바랬습니다. –
아마도 user.earned_skill? (스킬)과 같은 사용자 전용 메서드는 간단한 메모/캐싱을 수행 할 수 있습니다. 단 하나의 DB 쿼리로 작업 할 수 있다고 확신합니다. –
그게 내가 끝내 준거야. 'UserSkillsCache' 객체로 모든 것을 추출하고 거기서 메모했습니다. 감사! –