0
我有一個問題,我不知道如何解決。它與未分類的params散列有關。Foo.new中的Params順序(params [:foo]),需要一個在另一個之前(Rails)
我有一個對象當我設置時間=時,我也想通過外部服務器請求來驗證它,它具有虛擬時間=屬性和虛擬eating_session =屬性。我在method()方法的幫助下做了這樣的工作,它在其他服務器上進行查找並將所有可能的時間保存在@times變量中。
現在的問題是方法times()需要eating_session屬性來找出哪些時間是有效的,但rails有時會在Reservation對象中有任何eating_session之前先調用times =方法,當我只是執行@預訂= Reservation.new(PARAMS [:預訂])
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
我不知道如何解決這一問題,任何幫助apriciated。
這種情況會有何幫助?我不明白。 time =方法將在seat_session =之前調用,無論我是否驗證:validate_time在我的代碼中,還是我理解錯誤? – Jeena 2010-03-29 23:37:53
我已經刪除了你的時間=方法。問題在於,order params的處理是不確定的,但是你可以保證所有參數都設置後,驗證會運行。所以,我已將您的功能從訪問者移出並進入驗證。那有意義嗎? – Gareth 2010-03-30 06:27:58
但是我應該刪除:attr_accessor的時間:date,:time too? – Jeena 2010-03-30 09:40:46