rails to_json return single field

297 Views Asked by At

I'm using the method ".to_json" to render my result like this:

"this is a route in my controller"

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

I'm using this method to ask my DB:

"This is in my repository"

  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

My problem is: I want to get the "designation" from "article_designations" table with the "locale_id" 1. I have each time just one designation with the id 1.

So now I get this in my result:

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

But finaly I want this :

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

How can I proceed to have just the key/value and not key/value(array) => key/value?

Thanks

0

There are 0 best solutions below