N
N
nzk2018-08-30 14:47:32
Ruby on Rails
nzk, 2018-08-30 14:47:32

How can I implement filters on the Rails Api?

How can I make sure that certain filters are set on the rails and the client has already received filtered data from the server in advance (well, for example, the filter: name != null ). If this requires a serializer, I also use it.

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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Mirilaczvili, 2018-08-30
@2ord

If I understand correctly, then ActiveRecord has a where method specifically for this purpose.
Post.where.not(name: nil).order(:name)
But it is not clear why to store posts with empty names in the database at all.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question