3 Replies Latest reply on Sep 21, 2012 2:49 PM by Jon Tara

    Rho::RecordNotFound exception

      I'm creating a new controller method that looks up a record by a parameter other then ID.  Here is the code:

       

      @order = Order.find{|o| o.table_uid == @params[:table_id]}

       

      when I run this, my application crashes and I get the title error.  In the rhodes documentation here: http://docs.rhomobile.com/rhodes/application it indicates that this is the intended behavior, throw a 400 error if record not found.  However, the automatically generated code doesn't appear to have any of this error handling, and is simply:

       

      def show

          @order = Order.find(@params['id'])

          if @order

            render :action => :show, :back => url_for(:action => :index)

          else

            redirect :action => :index

          end

        end

       

      What am I missing here to properly handle an unfound find call?

        • Re: Rho::RecordNotFound exception
          Jon Tara

          Well, there's nothing "proper" about displaying a 400-error page for a record not found!

           

          Your controller should handle it. You have to decide what to do about it.

           

          Not sure why this would ever occur. Why would the UI be requesting a non-existing ID in the first place? It means you had to have generated a link with a non-existent ID. Sounds like something got deleted from the database, but not from the UI. (Maybe could happen due to sync?)

           

          That's not to say that Rhodes isn't broken. That's how I read that terse documentation as well.

            • Re: Rho::RecordNotFound exception

              For why this would occur:

              The page that is generating the links are a parent object, with the click action being IF child object exists show it ELSE redirect to new child object page. 

               

              Having the controller handle it is what I'm trying to do.  I imagine I can just use a rescue, but seeing how the autogenerated show method is written made me question if I really had to catch the exception (not sure if Rhodes is just automagically handling it for show) or if I was maybe doing something wrong in my call.

                • Re: Rho::RecordNotFound exception
                  Jon Tara

                  You don't need a rescue. In your controller, just retrieve by ID and test just as shown above.

                   

                  Normally, though, I would expect that if a child object doesn't exist, you wouldn't have an ID for it in the first place! If you delete some child object, you should clean-up the parent record by removing the child ID.

                   

                  Is this some "extended" data? i.e. you have a main table with maybe a few attributes, and then another table with additional attributes, both using the same primary key? So, then you just test to see if the extended data exists as I've suggested above.