Ruby on rails 4 Rails 4模型始终返回零
下面的错误让我发疯。我有一个用户类和它的两个属性(birth&created_at=>datetime)总是返回nil,而且它在数据库中有一个值 我正在使用Desive来管理身份验证 这是db表:Ruby on rails 4 Rails 4模型始终返回零,ruby-on-rails-4,rails-activerecord,Ruby On Rails 4,Rails Activerecord,下面的错误让我发疯。我有一个用户类和它的两个属性(birth&created_at=>datetime)总是返回nil,而且它在数据库中有一个值 我正在使用Desive来管理身份验证 这是db表: CREATE TABLE users ( id serial NOT NULL, email character varying(255) NOT NULL DEFAULT ''::character varying, encrypted_password character varyin
CREATE TABLE users
(
id serial NOT NULL,
email character varying(255) NOT NULL DEFAULT ''::character varying,
encrypted_password character varying(255) NOT NULL DEFAULT ''::character varying,
reset_password_token character varying(255),
reset_password_sent_at timestamp without time zone,
remember_created_at timestamp without time zone,
sign_in_count integer NOT NULL DEFAULT 0,
current_sign_in_at timestamp without time zone,
last_sign_in_at timestamp without time zone,
current_sign_in_ip character varying(255),
last_sign_in_ip character varying(255),
name character varying(255),
lastname character varying(255),
birthday timestamp without time zone,
gender character varying(255),
created_at timestamp without time zone,
updated_at timestamp without time zone,
authentication_token character varying(255),
facebook_uid character varying(255),
gplus_uid character varying(255),
CONSTRAINT users_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
这就是模型
#encoding: utf-8
class User < ActiveRecord::Base
has_many :user_benefits
has_many :benefits, through: :user_benefits
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates :gender, :inclusion => { :in => %w(m f)}
validates :email, email_format: { message: "Email invalido" }
before_save :ensure_authentication_token
def ensure_authentication_token
if authentication_token.blank?
self.authentication_token = generate_authentication_token
end
end
def set_gender new_gender
case new_gender.downcase
when "male"
self.gender = 'm'
when "female"
self.gender = 'f'
when "m"
self.gender = 'm'
when "f"
self.gender = 'f'
else
self.gender = nil
end
end
def get_response
{
:auth_token=>self.authentication_token,
:user => {
:email=> self.email,
:name=> self.name,
:lastname=> self.lastname,
:birthday=> self.birthday,
:gender=> self.gender,
:auth_token=> self.authentication_token,
:created_at=> self.created_at,
:updated_at=> self.updated_at,
:facebook_uid=> self.facebook_uid,
:gplus_uid=> self.gplus_uid
}
}
end
private
def generate_authentication_token
loop do
token = Devise.friendly_token
break token unless User.where(authentication_token: token).first
end
end
end
有人有主意吗
谢谢,我找到问题了
几天前,我更改了config/application.rb文件,添加了以下配置以解决时区的一些问题:
config.time_zone and config.active_record.default_timezone
我刚把它们取下来,一切都恢复正常。也有同样的问题,特别是
config.active_record.default_timezone
这导致activerecord将所有UTC存储的时间戳返回为null。不知道为什么,但删除它会带回所有数据。我可以保留默认的config.time\u区域。
config.active\u记录。默认时区需要一个符号,可以是:local,也可以是:utc
config.time\u zone
需要一个常规时区,就像您所做的那样
config.active\u record.default\u timezone
确定从数据库中提取日期和时间时是使用Time.local(如果设置为:local)还是使用Time.utc(如果设置为:utc)。默认值为:utc
config.active_record.default_timezone