Rails 播种布尔值不起作用
我将 Rails 3 与 Postgresql 一起使用,并且我有一个使用两个迁移定义的用户表(这是两个 self.up 方法):
def self.up
create_table(:users) do |t|
t.database_authenticatable :null => false
t.recoverable
t.rememberable
t.trackable
# t.confirmable
# t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
# t.token_authenticatable
t.timestamps
end
def self.up
add_column :users, :admin, :boolean, :default => false
end
现在,当我尝试使用管理员用户来播种它时,如下所示:
User.create(:username => "admin", :email => "[email protected]",:password => "password", :password_confirmation => "password", :admin => true)
它创建一个用户admin 等于 false,即使我指定为 true。我应该首先创建 User.new 并设置管理员还是一起删除默认值?
I'm using Rails 3 with Postgresql and I have a user table defined using two migrations (here are the two self.up methods):
def self.up
create_table(:users) do |t|
t.database_authenticatable :null => false
t.recoverable
t.rememberable
t.trackable
# t.confirmable
# t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
# t.token_authenticatable
t.timestamps
end
def self.up
add_column :users, :admin, :boolean, :default => false
end
Now when I go and try to seed this with an admin user like so:
User.create(:username => "admin", :email => "[email protected]",:password => "password", :password_confirmation => "password", :admin => true)
It creates a user with the admin equal to false, even though I specified true. Should I be first creating User.new and setting the admin or just get rid of the default all together?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 Rails 教程第 10 章: “只有 attr_accessible 属性可以通过批量分配来分配”,即通过添加 :admin =>;符合初始化哈希值。
您可以执行
User.create
,然后执行user.toggle!(:admin)
将您的特定用户设置为管理员。According to Rails Tutorial, Ch.10: "only attr_accessible attributes can be assigned through mass assignment", i.e., by adding :admin => true to the initialization hash.
You can do the
User.create
and then douser.toggle!(:admin)
to set your particular user as an admin.我非常确定问题不在于默认值,因为代码看起来没问题,您使用的是什么身份验证库?例如,我在使用 authlogic 时遇到过这样的问题。
I'm pretty much sure the problem is not with default value cause the code seems alright, what authentication lib are you using? I had problems like this with authlogic for example.