1

我正在使用带有 Postgresql 的 Rails 3,并且我有一个使用两个迁移定义的用户表(这是两个 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 => "foobar@gmail.com",:password => "password", :password_confirmation => "password", :admin => true)

即使我指定了 true,它也会创建一个 admin 等于 false 的用户。我应该首先创建 User.new 并设置管理员还是一起摆脱默认设置?

4

2 回答 2

3

根据Rails 教程,第 10 章:“只有 attr_accessible 属性可以通过批量分配来分配”,即通过将 :admin => true 添加到初始化哈希。

您可以执行User.create然后执行user.toggle!(:admin)将您的特定用户设置为管理员。

于 2011-05-25T16:03:14.210 回答
1

我很确定问题不在于默认值,因为代码看起来不错,您使用的是什么身份验证库?例如,我在使用 authlogic 时遇到了这样的问题。

于 2011-02-03T15:13:57.993 回答