1

我正在使用方法“.to_json”来呈现我的结果,如下所示:

“这是我控制器中的一条路线”

def show_articles
      render json: @company_repo.get_articles_by_company_id(params[:id])
                       .to_json(include: [{product_groups: {except: [:created_at, :updated_at]}},
                                          {ean13: {only: :code}},
                                          {article_designations: {only: :designation}}])
end

我正在使用这种方法来询问我的数据库:

“这是在我的存储库中”

  def get_articles_by_company_id(id)
    Article.all
        .joins(product_groups: [{laboratory: :company}])
        .joins(:ean13)
        .joins(:article_designations)
        .where('companies.id = ?', id)
        .where('article_designations.locale_id = 1')
  end

我的问题是:我想从具有“locale_id”1 的“article_designations”表中获取“名称”。我每次只有一个 ID 为 1 的名称。

所以现在我在我的结果中得到了这个:

"article_designations": [
    {
        "designation": "Procedente igitur mox tempore cum adventicium"
    }
]

但最后我想要这个:

"designation": "Procedente igitur mox tempore cum adventicium"

我怎样才能继续只有键/值而不是键/值(数组)=>键/值?

谢谢

4

0 回答 0