Вопрос

User Model:

class User < ActiveRecord::Base
 before_save { self.email == email.downcase }
 attr_accessor :name, :email

 validates :name, presence: true
 VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
 validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
                        uniqueness: { case_sensitive: false}

 has_secure_password
 validates :password, length: { minimum: 6 }
end

Inside of console:

:001 > User.all
User Load (3.1ms)  SELECT "users".* FROM "users"
=> #<ActiveRecord::Relation []>

:002 > User.create(name: "Ryan Waits", email: "ryan.waits@gmail.com", password:
"foobar", password_confirmation: "foobar")
(0.1ms)  begin transaction
User Exists (0.2ms)  SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") =
LOWER('ryan.waits@gmail.com') LIMIT 1
Binary data inserted for `string` type on column `password_digest`
SQL (6.8ms)  INSERT INTO "users" ("created_at", "password_digest", "updated_at") VALUES
(?, ?, ?)  [["created_at", Sat, 02 Nov 2013 16:20:10 UTC +00:00], ["password_digest",
"$2a$10$5MhI5u90tFqO3rrS58DW7eeNBdNCCveBA.IfqHp6OpwonYNaigyPO"], ["updated_at", Sat, 02
Nov 2013 16:20:10 UTC +00:00]]
(1.3ms)  commit transaction

=> #<User id: 3, name: nil, email: nil, created_at: "2013-11-02 16:20:10", updated_at:
"2013-11-02 16:20:10", password_digest:
"$2a$10$5MhI5u90tFqO3rrS58DW7eeNBdNCCveBA.IfqHp6Opwo..."> 

(user = User.all) user.name bring back "User" and the email and password attributes bring back an undefined method error on user object.

*Note:

I created another user with different attributes, same result, except the name, email, attributes return nil rather than "User" and undefined method error.

*Update:

So I now realize that the uniqueness validation is being hit with this message:

SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") =
LOWER('ryan.waits@gmail.com') LIMIT

But when I test a new user with new attributes, it still returns nil attributes when using the create method. However, updating the attributes like so (user.name = "adhsasd") will create a user. But then when I query User.all, the attributes are back to nil and the user is again invalid...am I hitting a different database somehow? I'm getting turned around...

Это было полезно?

Решение

Basically, I think you want attr_accessible instead of attr_accessor in your model...

From this answer

"attr_accessor is ruby code and is used when you do not have a column in your database, but still want to show a field in your forms. The only way to allow this is to attr_accessor :fieldname and you can use this field in your View, or model, if you wanted, but mostly in your View.

attr_accessible allows you to list all the columns you want to allow Mass Assignment, as andy eluded to above. The opposite of this is attr_protected which means this field i do NOT want anyone to be allowed to Mass Assign to. More then likely it is going to be a field in your database that you don't want anyone monkeying around with. Like a status field, or the like."

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top