对于Rails中的sqlite3,有些字段如" created_at"和" updated_at"返回为零

时间:2014-07-01 01:25:12

标签: ruby-on-rails activerecord sqlite

我正在使用Rails 4和sqlite3作为后端数据库进行项目,对于其中一个模型" Reservation",在我将记录保存到数据库之后,然后检索它,值字段" start_time"," end_time"," created_at"和" updated_at"返回为零。这个问题是否有任何可能的原因?

见下面的日志:

[3] pry(main)> a = Reservation.create(start_time: DateTime.now, seats: 2, user_id: 287, end_time: DateTime.now, restaurant_id: 88)

(0.1ms)  begin transaction

SQL (0.4ms)  INSERT INTO "reservations" ("created_at", "end_time", "restaurant_id",  
"seats", "start_time", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?)  
[["created_at", "2014-06-30 21:10:10.121316"], ["end_time", "2014-06-30 
21:10:10.115301"], ["restaurant_id", 88], ["seats", 2], ["start_time", "2014-06-30 
21:10:10.115191"], ["updated_at", "2014-06-30 21:10:10.121316"], ["user_id", 287]]
User Load (0.1ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = ? LIMIT 1  
[["id", 287]]
User Exists (0.1ms)  SELECT  1 AS one FROM "users"  WHERE ("users"."email" = 
'grace@seatyourself.com' AND "users"."id" != 287) LIMIT 1
SQL (0.1ms)  UPDATE "users" SET "loyalty_points" = ?, "updated_at" = ? WHERE 
"users"."id" = 287  [["loyalty_points", 20], ["updated_at", "2014-06-30 
21:10:10.146650"]]

(0.7ms)  commit transaction

=> #<Reservation id: 110, start_time: "2014-07-01 01:10:10", seats: 2, user_id: 287, 
created_at: "2014-07-01 01:10:10", updated_at: "2014-07-01 01:10:10", end_time: 
"2014-07-01 01:10:10", restaurant_id: 88>

[4] pry(main)> a
 => #<Reservation id: 110, start_time: "2014-07-01 01:10:10", seats: 2, user_id: 287, 
 created_at: "2014-07-01 01:10:10", updated_at: "2014-07-01 01:10:10", end_time: "2014-
 07-01 01:10:10", restaurant_id: 88>

 [5] pry(main)> b = Reservation.last
 Reservation Load (0.2ms)  SELECT  "reservations".* FROM "reservations"   ORDER BY 
 "reservations"."id" DESC LIMIT 1
 => #<Reservation id: 110, start_time: nil, seats: 2, user_id: 287, created_at: nil, 
 updated_at: nil, end_time: nil, restaurant_id: 88>

模型的架构:

 create_table "reservations", force: true do |t|
  t.datetime "start_time"
  t.integer  "seats"
  t.integer  "user_id"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.datetime "end_time"
  t.integer  "restaurant_id"
end

这是预留的模型(为了调试此问题,临时注释了验证)

 class Reservation < ActiveRecord::Base
  belongs_to :user
  belongs_to :restaurant

  after_create :add_loyalty_points #,:send_confirmation_email
  after_destroy :remove_loyalty_points

  #validates :start_time, :end_time, :seats, presence: true
  #validates :seats, numericality: {:greater_than_or_equal_to => 2, :less_than_or_equal_to => 20}

  #validate :reservation_only_last_for_an_hour

  #validate :reservation_should_not_exceed_restaurant_capacity

  #validate :reservation_should_not_outside_operating_hours

  # validates :start_time, time: {before: :end_time}
  # validates :end_time, time: {after: :start_time}

  def add_loyalty_points
    self.user.loyalty_points  += self.seats * 10
    self.user.save
  end

  def remove_loyalty_points
      if self.user.loyalty_points >=  self.seats * 10
         self.user.loyalty_points -= self.seats * 10
      else
         self.user.loyalty_points = 0
      end
      self.user.save
  end

  def send_confirmation_email
      self.user.send_confirmation
  end

  def reservation_should_not_outside_operating_hours
      return if  (start_time.nil?) or (end_time.nil?)

       if  start_time.hour < 11  or end_time.hour > 20  or (end_time.hour == 20 && (end_time.min > 0 || end_time.second > 0))
          errors[:base] << "Reservation timeslot cannot outside operating hours [11AM - 8 PM]"
          false
       else
          true
       end
  end

  def reservation_should_not_exceed_restaurant_capacity

      return if [start_time, end_time, restaurant.capacity].all?

      reserved_seats = restaurant.reservations.where("start_time = ?  AND end_time = ?", start_time,end_time).sum(:seats)

      if self.restaurant.capacity - reserved_seats >= seats
         true
      else
         errors[:base] << "There are not enough seats available at this time slot: you order #{seats}, but only #{restaurant.capacity - reserved_seats} are availabled"
      false
      end
  end

  def reservation_only_last_for_an_hour
      if ( !end_time.nil? && !start_time.nil? && (self.end_time - self.start_time).to_i - 3600> 0)
        errors .add(:duration_error, "the duration for a reservation should not last more than 1 hour") 
        false
      else
        true
      end
  end

end

0 个答案:

没有答案