加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 时间戳属性为零

发布时间:2020-12-17 03:24:15 所属栏目:百科 来源:网络整理
导读:我有2个型号,Microspost和用户: class Micropost ActiveRecord::Base belongs_to :user default_scope - { order(created_at: :desc) } validates :user_id,presence: true validates :content,presence: true,length: { maximum: 140 }endclass User Activ
我有2个型号,Microspost和用户:

class Micropost < ActiveRecord::Base
  belongs_to :user
  default_scope -> { order(created_at: :desc) }
  validates :user_id,presence: true
  validates :content,presence: true,length: { maximum: 140 }
end

class User < ActiveRecord::Base
  has_many :microposts,dependent: :destroy
  attr_accessor :remember_token,:activation_token,:reset_token
  before_save   :downcase_email
  before_create :create_activation_digest
  validates :name,length: { maximum: 50 }
end

seed.rb:

User.create!(name:  "Example User",email: "example@railstutorial.org",password:              "foobar",password_confirmation: "foobar",admin: true,activated:    true,activated_at: Time.zone.now)
99.times do |n|
  name  = Faker::Name.name
  email = "example-#{n+1}@railstutorial.org"
  password = "password"
  User.create!(name:  name,email: email,password:              password,password_confirmation: password,activated_at: Time.zone.now)  
end

# Microposts
users = User.order(:created_at).take(6)
50.times do
  content = Faker::Lorem.sentence(5)
  users.each { |user| user.microposts.create!(content: content) }
end

但是当我使用Faker创建Microposts时,在Rails中created_at和updated_at属性为nil但在posgresql控制台中不是.这让我困惑,我不知道如何解决它.
此外,当我手动创建帖子时,created_at属性不是nil.有人可以告诉我发生了什么事吗?

2.1.5 :016 >   m = Micropost.create!(content: "hello",user_id: User.first.id)
  User Load (0.6ms)  SELECT  "users".* FROM "users"  ORDER BY "users"."id" ASC LIMIT 1
   (0.2ms)  BEGIN
  SQL (0.8ms)  INSERT INTO "microposts" ("content","user_id","created_at","updated_at") VALUES ($1,$2,$3,$4) RETURNING "id"  [["content","hello"],["user_id",1],["created_at","2015-01-13 08:07:38.584269"],["updated_at","2015-01-13 08:07:38.584269"]]
   (9.6ms)  COMMIT
 => #<Micropost id: 301,content: "hello",user_id: 1,created_at: "2015-01-13 07:07:38",updated_at: "2015-01-13 07:07:38"> 
2.1.5 :017 > m.created_at
 => Tue,13 Jan 2015 08:07:38 CET +01:00

当然,在我的Posgresql控制台中,你可以看到微博确实有时间戳.

railsdays_development=# d+ microposts
                                                         Table "public.microposts"
   Column   |            Type             |                        Modifiers                        | Storage  | Stats target | Description 
------------+-----------------------------+---------------------------------------------------------+----------+--------------+-------------
 id         | integer                     | not null default nextval('microposts_id_seq'::regclass) | plain    |              | 
 content    | text                        |                                                         | extended |              | 
 user_id    | integer                     |                                                         | plain    |              | 
 created_at | timestamp without time zone | not null                                                | plain    |              | 
 updated_at | timestamp without time zone | not null                                                | plain    |              | 
Indexes:
    "microposts_pkey" PRIMARY KEY,btree (id)
    "index_microposts_on_user_id" btree (user_id)
    "index_microposts_on_user_id_and_created_at" btree (user_id,created_at)
Has OIDs: no

railsdays_development=# select * from microposts;
 id  |                                      content                                      | user_id |         created_at         |         updated_at         
-----+-----------------------------------------------------------------------------------+---------+----------------------------+----------------------------
   1 | Velit optio magni in modi distinctio.                                             |       1 | 2015-01-13 06:48:48.212602 | 2015-01-13 06:48:48.212602
   2 | Velit optio magni in modi distinctio.                                             |       2 | 2015-01-13 06:48:48.216021 | 2015-01-13 06:48:48.216021
   3 | Velit optio magni in modi distinctio.                                             |       3 | 2015-01-13 06:48:48.218617 | 2015-01-13 06:48:48.218617
   4 | Velit optio magni in modi distinctio.                                             |       4 | 2015-01-13 06:48:48.221544 | 2015-01-13 06:48:48.221544
   5 | Velit optio magni in modi distinctio.                                             |       5 | 2015-01-13 06:48:48.223975 | 2015-01-13 06:48:48.223975
   6 | Velit optio magni in modi distinctio.                                             |       6 | 2015-01-13 06:48:48.226611 | 2015-01-13 06:48:48.226611
   7 | Magni aliquid ut enim sunt aut.                                                   |       1 | 2015-01-13 06:48:48.22897  | 2015-01-13 06:48:48.22897
   8 | Magni aliquid ut enim sunt aut.                                                   |       2 | 2015-01-13 06:48:48.23096  | 2015-01-13 06:48:48.23096
   9 | Magni aliquid ut enim sunt aut.                                                   |       3 | 2015-01-13 06:48:48.232889 | 2015-01-13 06:48:48.232889
  10 | Magni aliquid ut enim sunt aut.                                                   |       4 | 2015-01-13 06:4:

Faker创建的Micropost没有时间戳.但我自己创建的Microposts有一个有效的时间戳.

这是来自Faker创建的微博(前300个微博)

2.1.5 :021 > Micropost.count
   (0.6ms)  SELECT COUNT(*) FROM "microposts"
 => 301 
2.1.5 :022 > a = Micropost.find(2).created_at
  Micropost Load (0.6ms)  SELECT  "microposts".* FROM "microposts" WHERE "microposts"."id" = $1  ORDER BY "microposts"."created_at" DESC LIMIT 1  [["id",2]]
 => nil 
2.1.5 :023 >

_create_microposts.rb:

class CreateMicroposts < ActiveRecord::Migration
  def change
    create_table :microposts do |t|
      t.text :content
      t.references :user,index: true

      t.timestamps null: false
    end
    add_index :microposts,[:user_id,:created_at]
  end
end

解决方法

在深入了解您的应用程序后,我发现您在config / application.rb中有这一行:

config.active_record.default_timezone =’华沙’

config.active_record.default_timezone要求:如果您希望使用UTC,则为utc;如果您希望它使用config.time_zone中找到的时区,则为:local.在本地,当我将其设置为:local时,我得到:

>> Micropost.first
  Micropost Load (1.0ms)  SELECT  "microposts".* FROM "microposts"  ORDER BY "microposts"."created_at" DESC LIMIT 1
=> #<Micropost id: 300,content: "Nemo fuga eveniet expedita consequatur.",user_id: 6,created_at: "2015-01-19 22:21:48",updated_at: "2015-01-19 22:21:48",picture: nil>

当设置为:utc时,我得到:

>> Micropost.first
  Micropost Load (0.6ms)  SELECT  "microposts".* FROM "microposts"  ORDER BY "microposts"."created_at" DESC LIMIT 1
=> #<Micropost id: 300,created_at: "2015-01-19 16:21:48",updated_at: "2015-01-19 16:21:48",picture: nil>

我可能会自己错过这个,但我去挖掘文档,因为配置“只是看起来不正确”.

相关文档可在section 3.6 of the Rails guide under Configuring Active Record.中找到

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读