這是我的用戶模型,它是適當的:爲什麼我的對象屬性在我的控制檯中顯示兩個不同的值? 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
爲實際的用戶對象,而不是在變量?
應該是self.plan_id而不是隻在before_save中的plan_id? – 2011-04-17 23:25:17
這是做同樣的事情。但據我瞭解,這並不重要。我很確定我嘗試過,結果是一樣的。將再次執行並確認。 – marcamillion 2011-04-17 23:26:36
另外,您可能需要before_create而不是before_save – 2011-04-17 23:27:51