0

Hi I have a model books with below mentioned method:

def self.text_search(query)
    pry
    if query.present?
        where("title @@ :q or author @@ :q", q: query)
    else
        find(-1)
    end
end

In my controller, I send respond to client as JSON:

render json: @books

This code works fine if the query returns something. If not, I am not able to send a JSON response. I get a template missint error. How can I handle it in JSON?

I tried

class BooksController < ApplicationController
    def index
        @books = Book.text_search(params[:query])
        @author= Author.find(Shelf.find(@books.map(&:isbn).uniq).map(&:author_id))
        rescue ActiveRecord::RecordNotFound  #donothing

        render json: @books
    end
end
4
  • Can you show the controller action also ? Commented Feb 29, 2016 at 7:48
  • Hi Arup I have updated the question with controller. I realised that the issue is with my next database call. Commented Feb 29, 2016 at 7:58
  • Are you having to rescue because of find(-1)? Commented Feb 29, 2016 at 7:59
  • no find(-1) give an empty book list. If Shelf has not books with the isbn we picked from books then , and for author I want to handle it Commented Feb 29, 2016 at 8:03

2 Answers 2

3
def self.text_search(query)
  if query.present?
    where("title @@ :q or author @@ :q", q: query)
  else
    Book.none
  end
end

should do the trick. Book.none returns an empty ActiveRecord::Relation.

In addition, i don't get why you assign authors while you only render @books.

@author= Author.find(Shelf.find(@books.map(&:isbn).uniq).map(&:author_id))

will raise ActiveRecord::RecordNotFound if no matching records present. Consider using find_by id: []

Sign up to request clarification or add additional context in comments.

2 Comments

Hi tobmath thanks for Book.none trick will use it but it doesnt apply in this case I guess. I am passing a query so if query.present? is valid and where clause is executed. and in where clause finds nothing.Shelf.find is upset. and for that I am getting Missing template error. I can ignore and return only books but I dont know how to handle silently exception for Shelf.find
I don't know how your models are set up, but @author = Author.find_by(id: Shelf.find(@books.map(&:isbn).uniq).map(&:author_id)) should work.
0

I like the thought from tobmatth. However, I also believe you should avoid throwing (and handling) exceptions in the normal (expected) flow of your code.

So, I'd also suggest something like:

class BooksController < ApplicationController
  def index
    @books = Book.text_search(params[:query])
    if @books.count > 0
      @author= Author.find(Shelf.find(@books.map(&:isbn).uniq).map(&:author_id))
    end

    render json: @books

  end
end

I think this would have worked with your original code (perhaps?).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.