轨道4:ActiveRecord的不保存任何属性,节约默认值 [英] Rails 4: ActiveRecord not saving any attributes, saving default values

查看:162
本文介绍了轨道4:ActiveRecord的不保存任何属性,节约默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户模型与某些属性,给他们打电话。所以,我的模型是这样的:

I have a User model with some attributes, call them foo and bar. So my model looks like this:

class User < ActiveRecord::Base
  attr_accessor :foo, :bar
end

然后我做到以下几点:

Then I do the following:

user = User.new
user.foo = "123"
user.save!

和我的开发日志显示:

INSERT INTO "users" DEFAULT VALUES RETURNING "id"

那么,如果我去到Rails的控制台,并做 User.first 我得到的是这样的:

Then if I go into the Rails console and do User.first I get something like:

#<User id: 4, foo: nil, bar: nil>

我使用Postgres的,我有没有烦恼节省其他车型到数据库中,为什么我的用户节能模式的默认值?

I am using Postgres and I am having no trouble saving other models to the database, why is my User model saving default values?

推荐答案

attr_accessor ​​是压倒一切的轨道属性。尝试删除它,它应该工作。

attr_accessor is overriding the rails attributes. Try removing it and it should work.

这篇关于轨道4:ActiveRecord的不保存任何属性,节约默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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