E
E
Earphone2018-08-31 12:28:23
Ruby on Rails
Earphone, 2018-08-31 12:28:23

How to prevent duplicate entries?

Let's say I have several hundred records in the database in one of the tables. And some of these records have the same name (name). So here's how to prevent so that records with the same name are not output in JSON. That is, suppose that there are 6 identifiers named "Manager" and of these six, only 1 should be displayed in json (which one does not matter). And also in json an element with the total number of entries is displayed, you need to make sure that the total number is also displayed but without duplicate entries.

class WorksController < ApplicationController

  def index
    works = paginate Work.all.order(:name), per_page: 10 
    render json:  { data: works.as_json, meta: { total: Work.count } }
  end

end

If you need I can use "serializers", just tell me how to do it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
TyzhSysAdmin, 2018-08-31
@POS_troi

You have a clear problem with the database architecture, this problem needs to be solved in this direction.
What you are trying to do is a bicycle, and already rusty through and through, before it even left the assembly line.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question