0
我有這個限制的問題!我不能比較兩個日期和控制檯給我這個錯誤:使用ActiveRecord :: Validator類時無法比較日期
undefined method `<' for nil:NilClass
這是我到目前爲止寫的代碼。
# app/models/reservation.rb
class CountValidator < ActiveModel::Validator
def validate(record)
if (record.second || record.first)
record.errors[:base]<< ' error '
end
end
end
class DepartureValidator < ActiveModel::Validator
def validate(record)
if (record.date_trip < Date.today)
record.errors[:base]<< ' error '
end
end
end
class Reservation < ActiveRecord::Base
validates_with DepartureValidator
validates_with CountValidator
belongs_to :dep ,:class_name => 'Stop', :foreign_key => 'dep_id'
belongs_to :arr ,:class_name => 'Stop',:foreign_key => 'arr_id'
belongs_to :route
belongs_to :user
delegate :CountStop, :to => :route, prefix: true, :allow_nil => false
delegate :city ,:to => :arr, :allow_nil => false
delegate :city ,:to => :dep, :allow_nil => false
def division
return Reservation.select{|r| r.route_id == route_id && r.date_trip == date_trip && r.id != id }
end
def second
if (class_point == 2)
y=division.select{ |l| l.class_point == 2 }.count
if(y+1 > route.train.II_class_seats)
return true
end
end
return false
end
def first
if (class_point == 1)
y=division.select{ |l| l.class_point == 1 }.count
if(y+1 > route.train.I_class_seats)
return true
end
end
return false
end
end
我不明白問題在哪裏。我的一位朋友寫了相同的代碼,但它對他完全起作用。
編輯:
這是完全錯誤的消息:正在與DepartureValidator驗證沒有一個「date_trip」屬性(如圖日誌文件中)
Started POST "/reservations" for 127.0.0.1 at 2015-09-02 15:04:13 +0200
Processing by ReservationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"PzfuWIWXqb6C3cCiI+fLendTadwR/kG/++3q6TRcgxLAUGyD1eFluBZB2SZZB3f0ZH6QiRyoj6py+3a6gfOg8A==", "reservation"=>{"class_point"=>"1", "route_id"=>"2", "dep_id"=>"4", "arr_id"=>"4"}, "commit"=>"Create reservation!"}
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
(0.2ms) begin transaction
(0.1ms) rollback transaction
Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.4ms)
NoMethodError (undefined method `<' for nil:NilClass):
app/models/reservation.rb:12:in `validate'
app/controllers/reservations_controller.rb:33:in `block in create'
app/controllers/reservations_controller.rb:32:in `create'
Rendered /home/railsuser/.rvm/gems/ruby-2.2.2/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (5.8ms)
Rendered /home/railsuser/.rvm/gems/ruby-2.2.2/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.8ms)
Rendered /home/railsuser/.rvm/gems/ruby-2.2.2/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.1ms)
Rendered /home/railsuser/.rvm/gems/ruby-2.2.2/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (24.9ms)
Rendered /home/railsuser/.rvm/gems/ruby-2.2.2/gems/web-console-2.2.1/lib/web_console/templates/_markup.html.erb (0.8ms)
Rendered /home/railsuser/.rvm/gems/ruby-2.2.2/gems/web-console-2.2.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.4ms)
Rendered /home/railsuser/.rvm/gems/ruby-2.2.2/gems/web-console-2.2.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.5ms)
Rendered /home/railsuser/.rvm/gems/ruby-2.2.2/gems/web-console-2.2.1/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.6ms)
Rendered /home/railsuser/.rvm/gems/ruby-2.2.2/gems/web-console-2.2.1/lib/web_console/templates/console.js.erb within layouts/javascript (16.8ms)
Rendered /home/railsuser/.rvm/gems/ruby-2.2.2/gems/web-console-2.2.1/lib/web_console/templates/main.js.erb within layouts/javascript (0.4ms)
Rendered /home/railsuser/.rvm/gems/ruby-2.2.2/gems/web-console-2.2.1/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.6ms)
Rendered /home/railsuser/.rvm/gems/ruby-2.2.2/gems/web-console-2.2.1/lib/web_console/templates/index.html.erb (36.2ms)
請問您的朋友有_exactly_相同的代碼?當你運行'diff'時會發生什麼?你的朋友是否使用相同版本的Ruby和Rails?請發佈_full_錯誤消息,包括一行代碼。 **在問題中發佈錯誤,而不是評論** –
onebree
另外,它看起來像你來自JavaScript等背景。當你做這些陳述時,你不需要把這個陳述包含在parens中。它們最好用於方法參數或更好地定義操作順序。 – onebree
好吧我發佈了完整的錯誤信息 – Pino