2011-04-17 64 views
0

這是我的用戶模型,它是適當的:爲什麼我的對象屬性在我的控制檯中顯示兩個不同的值? Rails 3中

# == Schema Information 
# Schema version: 20110412170916 
# 
# Table name: users 
# 
# id     :integer   not null, primary key 
# email    :string(255) 
# encrypted_password :string(128) 
# password_salt  :string(255) 
# reset_password_token :string(255) 
# remember_token  :string(255) 
# remember_created_at :datetime 
# sign_in_count  :integer 
# current_sign_in_at :datetime 
# last_sign_in_at  :datetime 
# current_sign_in_ip :string(255) 
# last_sign_in_ip  :string(255) 
# username    :string(255) 
# first_name   :string(255) 
# last_name   :string(255) 
# created_at   :datetime 
# updated_at   :datetime 
# invitation_token  :string(60) 
# invitation_sent_at :datetime 
# plan_id    :integer 
# current_state  :string(255) 
# confirmation_token :string(255) 
# confirmed_at   :datetime 
# confirmation_sent_at :datetime 
# space_used   :integer   default(0), not null 
# failed_attempts  :integer   default(0) 
# unlock_token   :string(255) 
# locked_at   :datetime 
# trial_end_date  :date 
# active_subscription :boolean 
# 

class User < ActiveRecord::Base 
    before_save :set_trial_end 

    def set_trial_end 
    plan = Plan.find(plan_id) 
    end_of_trial = self.created_at + plan.trial_duration.days 
    self.trial_end_date = end_of_trial 
    end 

end 

當我創建在控制檯的用戶,並保存它,它顯示了正確的條目。具體而言,trial_end_date對於第一個是正確的......即。變量admin,如下圖所示:

ruby-1.9.2-p0 > admin.save 
=> true 
ruby-1.9.2-p0 > admin 
=> #<User id: 19, email: "[email protected]", encrypted_password: "$2a$10$W0eMZ8mrRw85/bM0eYjkc.DimnMj4awD1VdkqfuQe4JZ...", password_salt: "$2a$10$W0eMZ8mrRw85/bM0eYjkc.", reset_password_token: nil, remember_token: nil, remember_created_at: nil, sign_in_count: nil, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, username: "test13", first_name: "Test", last_name: "Thirteen", created_at: "2011-04-17 22:20:08", updated_at: "2011-04-17 22:20:08", invitation_token: nil, invitation_sent_at: nil, plan_id: 4, current_state: nil, confirmation_token: "TqlstUs_YuOzZ8OTAZmx", confirmed_at: nil, confirmation_sent_at: "2011-04-17 22:20:08", space_used: 0, failed_attempts: 0, unlock_token: nil, locked_at: nil, trial_end_date: "2011-04-24 22:20:08", active_subscription: nil> 

但是,一旦我做User.last這是我所看到的,和user.trial_end_date設置爲nil

ruby-1.9.2-p0 > User.last 
=> #<User id: 19, email: "[email protected]", encrypted_password: "$2a$10$W0eMZ8mrRw85/bM0eYjkc.DimnMj4awD1VdkqfuQe4JZ...", password_salt: "$2a$10$W0eMZ8mrRw85/bM0eYjkc.", reset_password_token: nil, remember_token: nil, remember_created_at: nil, sign_in_count: nil, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, username: "test13", first_name: "Test", last_name: "Thirteen", created_at: "2011-04-17 22:20:08", updated_at: "2011-04-17 22:20:08", invitation_token: nil, invitation_sent_at: nil, plan_id: 4, current_state: nil, confirmation_token: "TqlstUs_YuOzZ8OTAZmx", confirmed_at: nil, confirmation_sent_at: "2011-04-17 22:20:08", space_used: 0, failed_attempts: 0, unlock_token: nil, locked_at: nil, trial_end_date: nil, active_subscription: nil> 

爲什麼trial_end_date保存爲nil爲實際的用戶對象,而不是在變量?

+0

應該是self.plan_id而不是隻在before_save中的plan_id? – 2011-04-17 23:25:17

+0

這是做同樣的事情。但據我瞭解,這並不重要。我很確定我嘗試過,結果是一樣的。將再次執行並確認。 – marcamillion 2011-04-17 23:26:36

+0

另外,您可能需要before_create而不是before_save – 2011-04-17 23:27:51

回答

2

你不能在before回調使用created_at,因爲它不會被設置。使用Time.nowDate.today

+0

工程就像一個魅力。謝謝。 – marcamillion 2011-04-18 01:55:39

2

您的數據庫記錄需要鍵入Date,而不是DateTime。

在類似的情況下,我小心地將AR var設置爲日期。

其他人也有同樣的問題。參見Ruby論壇msg

回覆:爲什麼檢查AR實例「admin」和User.last的不同?因爲「admin」是要保存到dbms的數據。 User.last是AR記錄的一個全新實例,已經使用dbms從填充了數據

建議的解決方案

變化

def set_trial_end 
    plan = Plan.find(plan_id) 
    end_of_trial = self.created_at + plan.trial_duration.days 
    self.trial_end_date = end_of_trial.to_date ### explicitly convert to date 
end 
+0

這看起來像解釋它,但這是我現在得到的錯誤:'NoMethodError:當你沒有想到它時,你有一個零對象! 您可能預期了Array的一個實例。 評論無評論時出現錯誤。+' 它說錯誤在這一行'end_of_trial = self.created_at + plan.trial_duration.days' – marcamillion 2011-04-18 01:09:59

+0

好吧,我把'self.created_at'改成了'Date.today',所以行內容爲:'end_of_trial = Date.today + plan.trial_duration.days'。但是,它仍然保存爲零。 – marcamillion 2011-04-18 01:19:02

+0

什麼類型是trial_duration?你應該只是添加一個簡單的整數Date.today。不是一天的價值。 – 2011-04-18 06:09:06

相關問題