Guest User

Untitled

a guest
Feb 12th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. create_table "users", :force => true do |t|
  2. t.string "email"
  3. t.string "password_hash"
  4. t.string "password_salt"
  5. t.datetime "created_at", :null => false
  6. t.datetime "updated_at", :null => false
  7.  
  8. attr_accessible :email, :password, :password_confirmation
  9. attr_accessor :password
  10. before_save :encrypt_password
  11. validates_confirmation_of :password
  12. validates_presence_of :password, :on => :create
  13. validates_presence_of :email
  14. validates_uniqueness_of :email
  15. .
  16. .
  17. .
  18.  
  19. user = User.new
  20. user.password # => no method error
  21.  
  22. user = User.new
  23. user.email # => nil
  24.  
  25. user = User.new
  26. user.password_confirmation # => nil
  27.  
  28. validates_confirmation_of :password
  29.  
  30. class User
  31. attr_accessor :password
  32. end
  33.  
  34. u = User.new
  35. u.password = "secret"
  36. u.password # => "secret"
  37.  
  38. class Account < ActiveRecord::Base
  39. # First, you define 2 attributes: "password" and "created_at"
  40. attr_accessor :password
  41. attr_accessor :created_at
  42.  
  43. # Now you say that you want "password" attribute
  44. # to be changeable via mass-assignment, while making
  45. # "created_at" to be non-changeable via mass-assignment
  46. attr_accessible :password
  47. end
  48.  
  49. a = Account.new
  50.  
  51. # Perform mass-assignment (which is usually done when you update
  52. # your model using the attributes submitted via a web form)
  53. a.update_attributes(:password => "secret", :created_at => Time.now)
  54.  
  55. a.password # => "secret"
  56. # "password" is changed
  57.  
  58. a.created_at # => nil
  59. # "created_at" remains not changed
  60.  
  61. a.created_at = Time.now
  62.  
  63. a.created_at # => 2012-09-16 10:03:14
Add Comment
Please, Sign In to add comment