2015-01-13 9 views
5

2 मॉडल, Microspost और User है:टाइमस्टैम्प गुण हैं नहीं के बराबर

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, presence: true, length: { maximum: 50 } 
end 

seed.rb:

User.create!(name: "Example User", 
      email: "[email protected]", 
      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: true, 
       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 

लेकिन जब मैं Microposts बनाने के लिए ठग इस्तेमाल किया, created_at और updated_at विशेषताओं रेल में nil हैं लेकिन posgresql कंसोल में नहीं। इससे मुझे भ्रमित कर दिया गया है और मुझे नहीं पता कि इसे कैसे ठीक किया जाए। इसके अलावा जब मैं मैन्युअल रूप से एक पोस्ट बनाता हूं, created_at विशेषता शून्य नहीं है। क्या कोई मुझे बता सकता है कि क्या हो रहा है?

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 कंसोल में आप देख सकते हैं कि microposts वास्तव में टाइम स्टांप की क्या ज़रूरत है।

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: 

फ़ेकर द्वारा निर्मित माइक्रोप्रोस्ट nil टाइमस्टैम्प है। लेकिन माइक्रोप्रोस्ट्स मैं खुद को एक वैध टाइमस्टैम्प बनाता हूं।

इस ठग द्वारा बनाई गई एक micropost से है (पहले 300 microposts)

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 
+0

में पाया जा सकता क्या आप मुझे दिखा सकते हैं, जहां आपके created_at नहीं के बराबर हो रही है? – Ajay

+0

'2.1.5: 021> माइक्रोप्रोस्ट.count (0.6ms)" माइक्रोप्रोस्ट "से COUNT (*) चुनें (0) => 301 2.1.5: 022> ए = माइक्रोप्रोस्ट.फिंड (2) .created_at माइक्रोप्रोस्ट लोड (0.6ms) "माइक्रोप्रोस्ट" चुनें। * "माइक्रोप्रोस्ट" से "माइक्रोप्रोस्ट"। "आईडी" = $ 1 "माइक्रोप्रोस्ट" द्वारा ऑर्डर करें। "बनाया गया" डीईएससी लिमिटेड 1 [["आईडी", 2]] => शून्य 2.1 .5: 023> '@ अजय – Emanuel

+0

कृपया अपनी माइग्रेशन फ़ाइल, XXXX_create_microposts.rb माइग्रेशन पेस्ट करें? – Ajay

उत्तर

6

अपने आवेदन के माध्यम से खुदाई के बाद, मैं पाया है कि आप में इस लाइन है कि आपके config/application.rb:

config.active_record.default_timezone = 'Warsaw'

config.active_record.default_timezone या तो :utc की अपेक्षा करता है यदि आप यूटीसी, या :local का उपयोग करना चाहते हैं, तो आप config.time_zone में पाए गए समय क्षेत्र का उपयोग करने की अपेक्षा करते हैं। स्थानीय स्तर पर, जब मैं :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, content: "Nemo fuga eveniet expedita consequatur.", user_id: 6, 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.

संबंधित मुद्दे