Rails HABTM在连接查询中嵌套数组?

时间:2017-03-31 15:41:32

标签: ruby-on-rails ruby activerecord

我正在使用Rails 5创建一个REST api,其中涉及authorsmedia之间的has_and_belongs_to_many关系。

以下是模型:

class Author < ApplicationRecord
  has_and_belongs_to_many :media
end

class Medium < ApplicationRecord
  has_and_belongs_to_many :authors
end

以下是迁移:

class CreateMedia < ActiveRecord::Migration[5.0]
  def change
    create_table :media do |t|
      t.string :title
      t.text :description
      t.datetime :published_date
      t.string :type
      t.integer :pages
      t.time :length
      t.string :site
      t.timestamps
    end
  end
end

class CreateAuthors < ActiveRecord::Migration[5.0]
  def change
    create_table :authors do |t|
      t.string :first_name
      t.string :last_name
      t.text :bio
      t.string :site    
      t.timestamps
    end
  end
end

class CreateJoinTableMediumAuthor < ActiveRecord::Migration[5.0]
  def change
    create_join_table :media, :authors do |t|
      t.index [:medium_id, :author_id]
      t.index [:author_id, :medium_id]
    end
  end
end

我遇到的问题是我想在制作一般查询时将作者嵌入媒体中,反之亦然,类似于您在Laravel with Collections中可以做的事情。我到处寻找,似乎无法找到解决方案。

为了使其更清晰,这是针对所有媒体(Medium.all.includes(:authors))的查询的当前示例JSON响应:

[
  {
    "id": 1,
    "title": "The Personal MBA",
    "description": "Master the art of business",
    "published_date": "2017-03-31T14:51:46.675Z",
    "pages": 400,
    "length": null,
    "site": null,
    "created_at": "2017-03-31T14:51:46.687Z",
    "updated_at": "2017-03-31T14:51:46.687Z"
  },
  {
    "id": 2,
    "title": "The Circle",
    "description": "A chlling and prophetic tale",
    "published_date": "2017-03-31T14:51:46.690Z",
    "pages": 333,
    "length": null,
    "site": null,
    "created_at": "2017-03-31T14:51:46.691Z",
    "updated_at": "2017-03-31T14:51:46.691Z"
  },
]

我希望得到这样的回复:

[
  {
    "id": 1,
    "title": "The Personal MBA",
    "description": "Master the art of business",
    "published_date": "2017-03-31T14:51:46.675Z",
    "pages": 400,
    "length": null,
    "site": null,
    "authors": [
      {
        "id": 1,
        "first_name": "Josh",
        "last_name": "Kaufman",
        "bio": "A business writer",
      },
      {
        "id": 2,
        "first_name": "Dave",
        "last_name": "Eggers",
        "bio": "fiction",
      }
    ]
    "created_at": "2017-03-31T14:51:46.687Z",
    "updated_at": "2017-03-31T14:51:46.687Z"
  },
  {
    "id": 2,
    "title": "The Circle",
    "description": "A chlling and prophetic tale",
    "published_date": "2017-03-31T14:51:46.690Z",
    "pages": 333,
    "length": null,
    "site": null,
    "authors": [
      {
        "id": 2,
        "first_name": "Dave",
        "last_name": "Eggers",
        "bio": "fiction",
      }
    ],
    "created_at": "2017-03-31T14:51:46.691Z",
    "updated_at": "2017-03-31T14:51:46.691Z"
  },
]

区别于响应中包含的authors数组。

1 个答案:

答案 0 :(得分:0)

myFunction([t1,1],[t2,1],[t3,1],[t4,0],...,[t50,0])

现在调用此class Medium < ApplicationRecord has_and_belongs_to_many :authors def details as_json( include: {authors: except: [:created_at, :updated_at]} ) end end 方法。示例details,您的作者将包含在该对象中。