Rails 4 模型返回始终为零 [英] Rails 4 model returns always nil

查看:37
本文介绍了Rails 4 模型返回始终为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我因以下错误而发疯.我有一个 User 类,它的两个属性(生日和 created_at => 日期时间)总是返回 nil,除了它在数据库上有一个值.

I am going crazy with the following error. I have a User class and two of its attributes (birthday & created_at => datetime) returns always nil besides it has a value on the database.

我正在使用 devise 来管理身份验证.

I'm using devise to manage the authentication.

这是数据库表:

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

当我调用 get_response 时,我得到:

When I call get_response I'm getting:

{
  auth_token: "fRoi1RPTuP64m55jXCSS"
  user: {
    email: "testg@habitues.com"
    name: "Lalo"
    lastname: "Landa"
    birthday: null
    gender: "m"
    auth_token: "fRoi1RPTuP64m55jXCSS"
    created_at: null
    updated_at: "2013-11-27T20:14:22.534-03:00"
    facebook_uid: null
    gplus_uid: null
  }
  new: false
}

有人有想法吗?

谢谢

推荐答案

我发现了问题.

几天前,我更改了 config/application.rb 文件,添加了以下配置以解决时区的一些问题:

Some days ago I changed the config/application.rb file adding the following configuration to solve some issues with timezones:

config.time_zone and config.active_record.default_timezone

我刚刚删除了它们,一切又恢复正常了.

I've just removed them and all is working fine again.

这篇关于Rails 4 模型返回始终为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆