Posts have many tags and Tags have many posts. In Rails, I'll typically need to make a Post_Tags model and migration to join the two models.
Using an EmberJS front-end, I'm not sure how to do the serializers and if a Post_Tags model is necessary.
The models in Ember:
// app/models/post.js
export default DS.Model.extend({
heading: DS.attr('string'),
content: DS.attr(''),
fullImageUrl: DS.string('author'),
thumbnailUrl: DS.attr('string'),
pageId: DS.belongsTo('page'),
tagIds: DS.hasMany('tag')
});
// app/models/tag.js
export default DS.Model.extend({
name: DS.attr('string'),
postIds: DS.hasMany('post')
});
The expected JSON with an ActiveModelAdapter should be:
"posts": [{
"id": 1,
"heading": "foo",
"content": ,
"full_image_url": "foo",
"thumbnail_url": "foo",
"page_id": <page id>,
"tag_ids": [<tag ids>] }],
"tags": [{
"id": 1,
"name": "foo",
"post_ids": [<post ids>]
}]
Do I still need a Post_Tags model that belongs_to :post and belongs_to :tag? Do I need a Post_Tags serializer? Or will just saying has_many :posts in the Tag serializer and vice versa be enough?
I have never used ActiveModel, so speaking solely from the Ember-Data point of view, you do not need a join model . Using the models you've posted, Ember-Data should be able to format the JSON the way you want. You can see some example on the ActiveModelSerializer API page .
EDIT: From the Rails side, it doesn't look like you need a join model. As I said, I've never used ActiveModel, but this article seems to say that a join model would be unnecessary in this situation.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.