Rails 播种布尔值不起作用

发布于 2024-10-15 20:01:35 字数 923 浏览 4 评论 0原文

我将 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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奶茶白久 2024-10-22 20:01:35

根据 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 do user.toggle!(:admin) to set your particular user as an admin.

白鸥掠海 2024-10-22 20:01:35

我非常确定问题不在于默认值,因为代码看起来没问题,您使用的是什么身份验证库?例如,我在使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文