开发者

Rails seeding a boolean value not working

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 sel开发者_运维技巧f.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 => "foobar@gmail.com",: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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜