1

我的 user.rb 看起来像这样:

class User < ActiveRecord::Base
  attr_accessible :password, :password_confirmation, :crypted_password, :username, :password_salt, :persistence_token
  acts_as_authentic
  has_many :assignments
  has_many :roles, :through => :assignments
  def role_symbols
    roles.map do |role|
      role.name.underscore.to_sym
    end
  end  
end

但是我在rails控制台中得到了这个:

x = User.create(:username => "foo", :password => "bar", :password_confirmation => "bar")
   (0.2ms)  BEGIN
  User Exists (0.4ms)  SELECT 1 AS one FROM "users" WHERE LOWER("users"."username") = LOWER('foo') LIMIT 1
  User Exists (0.6ms)  SELECT 1 AS one FROM "users" WHERE "users"."persistence_token" = 'e3c363da87d37fc7f17d3d1f4d4c851b93cf974ae614ec0b0cb9eb1935dd2dc2ccab34d0a06b4580881dc38e5b65027cce0f2e53a005860fc81d7f5e4c245d6a' LIMIT 1
   (0.3ms)  ROLLBACK
+----+----------+---------+---------+---------+-------+---------+---------+
| id | username | cryp... | pass... | pers... | email | crea... | upda... |
+----+----------+---------+---------+---------+-------+---------+---------+
|    | foo      | 6196... | vIzV... | e3c3... |       |         |         |
+----+----------+---------+---------+---------+-------+---------+---------+
1 row in set
1.9.3-p194 :031 > User.all
  User Load (1.5ms)  SELECT "users".* FROM "users" 
 => [] 
1.9.3-p194 :032 > x.save
   (0.1ms)  BEGIN
  User Exists (0.6ms)  SELECT 1 AS one FROM "users" WHERE LOWER("users"."username") = LOWER('foo') LIMIT 1
  User Exists (0.3ms)  SELECT 1 AS one FROM "users" WHERE "users"."persistence_token" = 'e3c363da87d37fc7f17d3d1f4d4c851b93cf974ae614ec0b0cb9eb1935dd2dc2ccab34d0a06b4580881dc38e5b65027cce0f2e53a005860fc81d7f5e4c245d6a' LIMIT 1
   (0.1ms)  ROLLBACK
 => false 
4

1 回答 1

2

意识到 :email 需要一个 attr_accessible。

于 2012-10-19T09:38:00.777 回答