1

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

Jonathan
  • 51
  • 7
  • which version of rails are you using? – mayorsanmayor Oct 11 '18 at 11:09
  • I'm using "Rails 5.1.6" – Jonathan Oct 11 '18 at 11:10
  • You can try .as_json(root: false) as per this doc - https://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html can you try that let me know if it works – mayorsanmayor Oct 11 '18 at 11:12
  • also here, I notice this is what added the root label - `{article_designations: {only: :designation}}` after removing the root i.e `root: false`, you are going to want to change `article_designations: {only: :designation}` to `designation: {only: :designation}` – mayorsanmayor Oct 11 '18 at 11:17
  • What do you mean with ".as_json(root: false)"? chain this after my ".to_json(include: ......" ) ? Like this : .to_json(include: ...... ).as_json(root: false)? – Jonathan Oct 11 '18 at 11:22
  • I tried like this: render json: @company_repo.get_articles_by_company_id(params[:id]) .as_json(root: false, include: [{product_groups: {except: [:created_at, :updated_at]}}, {ean13: {only: :code}}, designation: {only: :designation}]) but no results – Jonathan Oct 11 '18 at 11:34
  • do you know this gem? - https://github.com/rails-api/active_model_serializers – mayorsanmayor Oct 11 '18 at 11:42
  • that gem gives u full control of how u want your models to look like, hence you will save yourself from using too much `include`, `only` and `exclude` - https://github.com/rails-api/active_model_serializers/blob/v0.10.6/docs/general/serializers.md#root You can see the use of root in that link. – mayorsanmayor Oct 11 '18 at 11:44
  • find full documentation here, I hope you will find it useful - https://github.com/rails-api/active_model_serializers/blob/v0.10.6/docs/README.md – mayorsanmayor Oct 11 '18 at 11:49
  • No I don't know it! thanks for the link, I would like to read the doc. Do you know https://github.com/rails/jbuilder ? probably I can work on the structure of the json but I don't know if it's a good idea. What do you think? – Jonathan Oct 11 '18 at 12:02
  • Better to use ActiveModelSerializer. I've used it on multiple projects. Never really used jbuilder so I don't know. But from what I see, it looks like with jbuilder you get to write more code. – mayorsanmayor Oct 11 '18 at 12:18
  • Ok, thanks a lot for your responses! I would check your solution "active_model_serializers". Thanks – Jonathan Oct 11 '18 at 12:20

0 Answers0