ActiveRecord不保存任何属性,保存默认值 [英] ActiveRecord not saving any attributes, saving default values

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

问题描述

我有一个带有某些属性的 User 模型,将它们称为 foo bar 。因此,我的模型如下所示:

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 覆盖了rails属性。尝试删除它,它应该可以工作。

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

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

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