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 711207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:42:32+00:00 2026-05-14T04:42:32+00:00

I have a problem which I don’t know how to fix. It has to

  • 0

I have a problem which I don’t know how to fix. It has to do with the unsorted params hash.

I have a object Reservation which has a virtual time= attribute and a virtual eating_session= attribute when I set the time= I also want to validate it via an external server request. I do that with help of the method times() which makes a lookup on a other server and saves all possible times in the @times variable.

The problem now is that the method times() needs the eating_session attribute to find out which times are valid, but rails sometimes calls the times= method first, before there is any eating_session in the Reservation object when I just do @reservation = Reservation.new(params[:reservation])

class ReservationsController < ApplicationController
    def new
        @reservation = Reservation.new(params[:reservation])
        # ...
    end
end

class Reservation < ActiveRecord::Base
    include SoapClient
    attr_accessor :date, :time
    belongs_to :eating_session

    def time=(time)
        @time = times.find { |t| t[:time] == time }
    end

    def times
        return @times if defined? @times
        @times = []
        response = call_soap :search_availability {
            # eating_session is sometimes nil
            :session_id => eating_session.code, # <- HERE IS THE PROBLEM
            :dining_date => date
        }
        response[:result].each do |result|
            @times << {
                :time => "#{DateTime.parse(result[:time]).strftime("%H:%M")}",
                :correlation_data => result[:correlation_data]
            }
        end
        @times
    end
end

I have no idea how to fix this, any help is apriciated.

  • 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-05-14T04:42:32+00:00Added an answer on May 14, 2026 at 4:42 am

    What I often do in this case is push the dependent code into a validation:

    class ReservationsController < ApplicationController
        def new
            @reservation = Reservation.new(params[:reservation])
            # ...
        end
    end
    
    class Reservation < ActiveRecord::Base
        include SoapClient
        attr_accessor :date, :time
        belongs_to :eating_session
        validate :validate_time
    
        def validate_time
          errors.add_to_base "Time is invalid" unless @time = times.find { |t| t[:time] == time }
        end
    
        def times
            return @times if defined? @times
            @times = []
            response = call_soap :search_availability {
                # eating_session is sometimes nil
                :session_id => eating_session.code, # <- HERE IS THE PROBLEM
                :dining_date => date
            }
            response[:result].each do |result|
                @times << {
                    :time => "#{DateTime.parse(result[:time]).strftime("%H:%M")}",
                    :correlation_data => result[:correlation_data]
                }
            end
            @times
        end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.