This is my User model that is appropriate:
# == 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
When I created a user at the console, and saved it, it shows the right entry. Specifically the trial_end_date
is correct for the first one....i.e. the variable admin
as seen below:
ruby-1.9.2-p0 > admin.save
=> true
ruby-1.9.2-p0 > admin
=> #<User id: 19, email: "test13@abc.com", 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_开发者_如何学Goat: 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>
But once I do User.last
this is what I see, and user.trial_end_date
is set to nil
.
ruby-1.9.2-p0 > User.last
=> #<User id: 19, email: "test13@abc.com", 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>
Why does trial_end_date
save it as nil
for the actual User object and not in the variable ?
You can't use created_at
in a before
callback because it won't be set. Use Time.now
or Date.today
.
Your database record wants type Date, not DateTime.
In similar situations, I'm careful to set the AR var to be a Date.
Other people have had the same problem. See Ruby forum msg
Re: Why do the Inspections of the AR instances "admin" and User.last differ? Because "admin" is the data that you want saved to the dbms. User.last is an entirely new instance of an AR record that has been populated with data from the dbms.
Suggested Solution
Change
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
精彩评论