Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8971363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:56:10+00:00 2026-06-15T17:56:10+00:00

I have an API which is fairly restful but am struggling to work out

  • 0

I have an API which is fairly restful but am struggling to work out how to implement a search cleanly. I want to be able to search for all the records between two date-times, the date-times are allowed to be a maximum of 6 hours apart. At the moment in my controller method I have the following:

required_params = [:start_time, :end_time]
if check_required_params(required_params, params) and check_max_time_bound(params, 6.hours)
   ... rest of controller code here ...
end

check_required_params is an application method that looks like this:

def check_required_params(required_params, params_sent)
required_params.each do |param|
  unless has_param(param, params_sent)
    unprocessable_entity
    return false
  end
end
  true
end

check_max_time is fairly similar.

I know it’s against best practices to do validation in the controller but I can’t see how I can add it to the model cleanly.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:56:11+00:00Added an answer on June 15, 2026 at 5:56 pm

    Actually what you are doing is (almost) best practice and will (almost) be incorporated in Rails 4 with strong parametsers. (I say almost because your check_max_time looks like it should be a validation in your model.)

    You should go ahead and pull in the feature today and make upgrades easier on yourself. Strong Parameters https://github.com/rails/strong_parameters

    Documentation is there, but here is how you incorporate it.

    class SearchController < ApplicationController
      include ActiveModel::ForbiddenAttributesProtection
    
      def create
        # Doesn't have to be an ActiveRecord model
        @results = Search.create(search_params)
        respond_with @results
      end
    
      private
    
      def search_params
        # This will ensure that you have :start_time and :end_time, but will allow :foo and :bar
        params.require(:start_time, :end_time).permit(:foo, :bar #, whatever else)
      end
    end
    
    class Search < ActiveRecord::Base
      validates :time_less_than_six_hours
    
      private
    
      def time_less_than_six_hours
        errors.add(:end_time, "should be less than 6 hours from start") if (end_time - start_time) > 6.hours
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fairly simple requirement for a query API which I need to
I have a REST API which is fairly typical, except that the id's of
I have an api which takes uni code data as c character array and
I have a scenario in which I have REST API which manages a Resource
I have a REST api which dumps some json data (user info, etc). Now
I have the win32 API CommandLineToArgvW which returns a LPWSTR* and warns me that
I have a web site with an API which publishes the information using JSON.
I have to deal with an API which need to be provided a DataSource
I'm developing an API which will also have an authentication/authorization component. Anybody, regardless of
I have some text. Is there is any api which can tell me the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.