基于Rails 5 API,如何在Ember 3中创建关联表模型?
Rails 5 API 与 Ember 3 配合的模型配置整理
我把你当前的Rails端模型和序列化器代码整理成清晰的格式,方便你查看和调试:
1. Rails 模型定义
ShopLanguage 关联模型
class ShopLanguage < ApplicationRecord belongs_to :shop belongs_to :language end
Shop 主模型
class Shop < ApplicationRecord has_many :shop_languages, inverse_of: :shop, dependent: :destroy has_many :languages, through: :shop_languages end
2. ShopLanguage 序列化器
你提供的序列化器内容有点截断,我先保留现有可识别的部分:
class ShopLanguageSerializer < ActiveModel::Serializer attributes :id, :shop_id, :language_id, :modified_at # 推测原内容的"modif..."是类似modified_at的时间字段 end
和Ember 3配合的小建议
如果要让Ember Data正确识别并解析关联关系,建议在序列化器里显式声明关联:
class ShopLanguageSerializer < ActiveModel::Serializer attributes :id, :shop_id, :language_id, :modified_at # 添加关联序列化规则,让返回的JSON符合Ember预期格式 belongs_to :shop belongs_to :language end
这样接口返回的JSON会包含关联资源的对应信息,能帮前端更顺畅地处理数据关联逻辑。
内容的提问来源于stack exchange,提问作者belgoros
相关产品推荐
相关产品推荐

